系统程序员成长计划-你的数据放在哪里(下)

About... 李先静

This author published 367 posts in this site.

Share

FacebookTwitterEmailWindows LiveTechnoratiDeliciousDiggStumbleponMyspaceLikedin

Comments


yetiboy
November 24th, 2008

画个图可能直观点:
在文件中时:
+—– +
|magic|
|No. |
+—– +
|other |
|stuff |
+—– +
|bss |
|size |
+—– +
|DATA |
| |
+—– +
|TEXT |
| |
+—– +

在进程空间中时:
+——+
|stack |
| |
+——+
|hole |
| |
+——+
|heap |
| |
+——+
|BSS |
| |
+——+
|DATA |
| |
+——+
|TEXT |
| |
+——+
|hole |
| |
+——+


yetiboy
November 25th, 2008

额,从Emacs中拷过来就变成这样的了。。。自动把空格个处理了,呵呵


Dig
November 25th, 2008

学习中。
“在x86平台上,栈顶寄存器为ESP,那么ESP的值在是PUSH操作之前修改呢,还是在PUSH操作之后修改呢?PUSH ESP这条指令会向栈中存入什么数据呢?据说x86系列CPU中,除了286外,都是先修改ESP,再压栈的。由于286没有CPUID指令,有的OS用这种方法检查286的型号”这话好像在先进之前的博文上看见过。

顺便请教下,如果用const 修饰的变量都放在.rodata中,是不是 const char * 这样的变量所占的空间就无法释放了?


Dig
November 25th, 2008

冒昧再请教一个问题,关于“以后每次写完程序都应该用valgrind跑一遍“的。写过demo,使用glib 的g_list_创建一个链表,随便加点东西,再释放,最后g_list_free,就会报告又内存泄漏,请问是为什么呢?
#include nbsp;

void
free_list_node nbsp;(gpointer nbsp;data, nbsp;gpointer nbsp;user_data)
{
nbsp; nbsp;if nbsp;(data nbsp;!= nbsp;NULL)
nbsp; nbsp; nbsp; nbsp;{
nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;g_free nbsp;(data);
nbsp; nbsp; nbsp; nbsp;}
}

void
print_list_node nbsp;(gpointer nbsp;data, nbsp;gpointer nbsp;user_data)
{
nbsp; nbsp;gchar nbsp;*str nbsp;= nbsp;(gchar nbsp;*) nbsp;data;
nbsp; nbsp;g_print nbsp;(“%s\n”, nbsp;str);
}

int
main nbsp;(int nbsp;argc, nbsp;char nbsp;**argv)
{
nbsp; nbsp;GList nbsp;*list nbsp;= nbsp;NULL;

nbsp; nbsp;list nbsp;= nbsp;g_list_append nbsp;(list, nbsp;g_strdup nbsp;(“abc”));
nbsp; nbsp;list nbsp;= nbsp;g_list_append nbsp;(list, nbsp;g_strdup nbsp;(“def”));
nbsp; nbsp;list nbsp;= nbsp;g_list_append nbsp;(list, nbsp;g_strdup nbsp;(“ghi”));

nbsp; nbsp;g_list_foreach nbsp;(list, nbsp;print_list_node, nbsp;NULL);
nbsp; nbsp;g_list_foreach nbsp;(list, nbsp;free_list_node, nbsp;NULL);

nbsp; nbsp;g_list_free nbsp;(list);

nbsp; nbsp;return nbsp;0;
}

#valgrind –leak-check=full ./g_list
==9432== LEAK SUMMARY:
==9432== definitely lost: 0 bytes in 0 blocks.
==9432== possibly lost: 744 bytes in 3 blocks.


Dig
November 25th, 2008

败了。。。
#include <glib.h>

void
free_list_node (gpointer data, gpointer user_data)
{
  if (data != NULL)
    {
      g_free (data);
    }
}

void
print_list_node (gpointer data, gpointer user_data)
{
  gchar *str = (gchar *) data;
  g_print (“%s\n”, str);
}

int
main (int argc, char **argv)
{
  GList *list = NULL;

  list = g_list_append (list, g_strdup (“abc”));
  list = g_list_append (list, g_strdup (“def”));
  list = g_list_append (list, g_strdup (“ghi”));

  g_list_foreach (list, print_list_node, NULL);
  g_list_foreach (list, free_list_node, NULL);

  g_list_free (list);

  return 0;
}


admin
November 25th, 2008

to yetiboy : 谢谢补充。
to dig: 跑glib的程序要禁掉它的内存管理机制,我有篇BLOG讲过(我忘记了,你找找)。 possibly lost的也可以不用管它。


lyb
March 18th, 2009

可能的泄露是gobject那套东西(类型注册、管理)引起的, 不用担心

[...] 拥抱变化(上)(下) 1.5 Don’t Repeat Yourself(DRY) (上)(下) 1.6 你的数据放在哪里(上)(下) 第2章 写得又快又好的秘诀 完成 2.1 好与快的关系(上)(下) 2.2 代码阅读法 [...]

Leave a comment