一、代码
1.1 代码
#include <stdlib.h> #include <stdio.h> int main(int argc, char*argv[]) { int p = (int)malloc(sizeof(int)); p = 1234; printf("p:%d\n",p); return 0; }
1.2 编译代码
gcc -c test test.c
二、valgrind使用
2.1 运行代码
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./test
2.2 结果
2.3 结果分析
从上文“total heap usage: 1 allocs, 0 frees, 4 bytes allocated"和"definitely lost: 4 bytes in 1 blocks”可以看出:
有4个字节的内存泄漏。