【发布时间】:2014-08-30 19:01:43
【问题描述】:
我正在尝试使用 malloc 函数为链表中的节点分配内存。但是,我在 malloc 调用时遇到分段错误。我无法理解 valgrind 生成的报告。
==28861== total heap usage: 76 allocs, 73 frees, 14,544 bytes allocated
==28861==
==28861== 48 bytes in 1 blocks are still reachable in loss record 1 of 3
==28861== at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==28861== by 0x4027A2: create_server_entry_into_connection_list (all.c:734)
==28861== by 0x401BF8: server_call (all.c:410)
==28861== by 0x40103F: main (all.c:108)
==28861==
==28861== 568 bytes in 1 blocks are still reachable in loss record 2 of 3
==28861== at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==28861== by 0x3E3C260309: __fopen_internal (in /lib64/libc-2.5.so)
==28861== by 0x4012E2: myip (all.c:174)
==28861== by 0x400FE7: main (all.c:101)
==28861==
==28861== 1,024 bytes in 1 blocks are still reachable in loss record 3 of 3
==28861== at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==28861== by 0x4027B3: create_server_entry_into_connection_list (all.c:736)
==28861== by 0x401BF8: server_call (all.c:410)
==28861== by 0x40103F: main (all.c:108)
==28861==
==28861== LEAK SUMMARY:
==28861== definitely lost: 0 bytes in 0 blocks
==28861== indirectly lost: 0 bytes in 0 blocks
==28861== possibly lost: 0 bytes in 0 blocks
==28861== still reachable: 1,640 bytes in 3 blocks
==28861== suppressed: 0 bytes in 0 blocks
==28861==
==28861== For counts of detected and suppressed errors, rerun with: -v
==28861== Use --track-origins=yes to see where uninitialised values come from
==28861== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)
Segmentation fault
谁能解释这种错误背后的原因。
【问题讨论】:
-
行号是一个好的开始...虽然我们不知道您的代码
-
实际上这是一个非常大的代码,连接了许多文件,因此无法在此处发布。我确实检查了这些行,但似乎我已经巧妙地使用了这些变量。此外,我在 valgrind 报告中读到,仍然可以访问标志在运行时不应与您的代码混淆,但我仍然遇到段错误。
标签: c memory-management segmentation-fault malloc valgrind