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 --tool=callgrind ./prog_name
callgrind_annotate callgrind.out 轉換爲文本