【发布时间】:2011-12-03 00:20:12
【问题描述】:
==20420==
==20420== HEAP SUMMARY:
==20420== in use at exit: 0 bytes in 1 blocks
==20420== total heap usage: 1 allocs, 0 frees, 0 bytes allocated
==20420==
==20420== Searching for pointers to 1 not-freed blocks
==20420== Checked 48,492 bytes
==20420==
==20420== 0 bytes in 1 blocks are still reachable in loss record 1 of 1
==20420== at 0x400677E: malloc (vg_replace_malloc.c:195)
==20420== by 0x80483D8: main (jig.c:10)
==20420==
==20420== LEAK SUMMARY:
==20420== definitely lost: 0 bytes in 0 blocks
==20420== indirectly lost: 0 bytes in 0 blocks
==20420== possibly lost: 0 bytes in 0 blocks
==20420== still reachable: 0 bytes in 1 blocks
==20420== suppressed: 0 bytes in 0 blocks
在我的项目中看到我这样使用 malloc:
malloc(sizeof(some_structure) * some_expression);
在某一时刻 some_expression 给出的值是 0,所以我间接地在做
malloc(0)
所以当我不打算 malloc 一个字节时,我不会释放它,但在这种情况下,valgrind 会显示内存泄漏。为什么?
编辑:
如果我这样使用:
char *a = malloc(0);
那么 a 不为 NULL。所以问题是为什么不为NULL? & 它存储哪个地址?
【问题讨论】:
标签: c memory-leaks malloc valgrind