我无法理解为什么valgrind(版本3.14)不能检测到此程序中可能存在的内存泄漏:
#include <stdlib.h>
int main() {
int *p = malloc(sizeof(int));
p = realloc(p, 2 * sizeof(int));
free(p);
return 0;
}
C99标准(ISO/IEC 9899:1999,第314页)关于realloc的说明如下:
如果不能为新对象分配内存,则不会释放旧对象,并且其值不变。realloc函数返回指向新对象的指针(可能与指向旧对象的指针具有相同的值),或者如果无法分配新对象则返回空指针。
因此,可能出现p为NULL但先前使用malloc分配的内存单元仍然存在的情况,这不应该是可能的内存泄漏吗?
如果我使用gcc -std=c99编译程序,并使用valgrind --tool=memcheck --leak-check=full --track-origins=yes运行它,则会显示以下消息:
==313618== Memcheck, a memory error detector
==313618== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==313618== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==313618== Command: ./a.out
==313618==
==313618==
==313618== HEAP SUMMARY:
==313618== in use at exit: 0 bytes in 0 blocks
==313618== total heap usage: 2 allocs, 2 frees, 12 bytes allocated
==313618==
==313618== All heap blocks were freed -- no leaks are possible
==313618==
==313618== For counts of detected and suppressed errors, rerun with: -v
==313618== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
realloc
的返回值分配给一个临时变量进行测试,然后再覆盖原指针的一个好理由。如果返回NULL
,你的恢复操作可以释放原始指针,而不会丢失它。 - Weather Vane