安装
apt-get install valgrind
测试程序
#include <stdio.h> #include <stdlib.h> int*Test(void) { int* x = malloc(10 * sizeof(int)); delete x;// problem 1: heap block overrun, problem 2: memory leak --x not free, only first address return x; } int main(void) { int count; Test(); printf("i =%d/n", count); //problem 3: use uninitialised value. return 0; }
内存检查命令
valgrind --tool=memcheck ./prog_name
valgrind --tool=memcheck --leak-check=yes ./prog_name
输出内存泄露的申请堆栈
valgrind --db-attach=yes --tool=memcheck ./prog_name
出错后附加gdb
时间: 2024-10-16 22:31:10