首页 > 代码库 > valgrind内存检查

valgrind内存检查

安装
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

valgrind内存检查