C Valgrind输出理解
在我的项目中,我使用malloc如下:C Valgrind输出理解,c,memory-leaks,malloc,valgrind,C,Memory Leaks,Malloc,Valgrind,在我的项目中,我使用malloc如下: ==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== Ch
==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(0)
在某个点上,某个_表达式给出了值0,所以我间接地这样做
malloc(sizeof(some_structure) * some_expression);
所以,当我不打算malloc一个字节,所以我不会释放它,但在这种情况下,valgrind显示内存泄漏。为什么?
编辑:
如果我这样使用:
==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(0)
那么a不是空的。所以问题是为什么不为空它存储哪个地址?来自mymalloc(3)
manpage(Linux):
如果大小为0,则malloc()
返回NULL
,或者返回一个唯一的指针值,该值可以在以后成功传递到free()
因此,无法保证当传递0时,malloc
不会分配任何空间,如果它不是NULL
,则必须释放它给您的指针
如果malloc
未返回NULL
,则会得到一个不能用于任何内容的缓冲区,但由于它具有唯一地址,malloc
必须至少分配一个字节
也许您希望将malloc
调用替换为一对一
char *a = malloc(0);
从我的malloc(3)
manpage(Linux)中:
如果大小为0,则malloc()
返回NULL
,或者返回一个唯一的指针值,该值可以在以后成功传递到free()
因此,无法保证当传递0时,malloc
不会分配任何空间,如果它不是NULL
,则必须释放它给您的指针
如果malloc
未返回NULL
,则会得到一个不能用于任何内容的缓冲区,但由于它具有唯一地址,malloc
必须至少分配一个字节
也许您希望将malloc
调用替换为一对一
char *a = malloc(0);
可能的重复可能的重复