valgrind:a suite of tools for debugging and profiling programs
1)简介:Valgrind用于调试、分析Linux的可执行文件。
2)安装:
yum install valgrind.x86_64 # yum search valgrind
3)常用选项:--tool=<toolname>:运行Valgrind的工具。默认是memcheck,其他可用值有cachegrind、callgrind等。
4)示例:
int main() { int *p1 = new int; int *p2 = NULL; cout << *p2 << endl; return 0; }
编译(加-g选项)得到a.out,再执行valgrind ./a.out,以下是其输出:
==16564== Memcheck, a memory error detector # 默认使用memcheck工具 ... ... ==16564== Invalid read of size 4 # 在test.cpp第9行出现非法内存访问(且size为4) ==16564== at 0x4008B2: main (test.cpp:9) ==16564== Address 0x0 is not stack‘d, malloc‘d or (recently) free‘d ... ... ==16564== Process terminating with default action of signal 11 (SIGSEGV) ==16564== Access not within mapped region at address 0x0 ==16564== at 0x4008B2: main (test.cpp:9) ... ... ==16564== HEAP SUMMARY: ==16564== in use at exit: 4 bytes in 1 blocks # 内存泄漏情况 ==16564== total heap usage: 1 allocs, 0 frees, 4 bytes allocated ==16564== ==16564== LEAK SUMMARY: ==16564== definitely lost: 0 bytes in 0 blocks ==16564== indirectly lost: 0 bytes in 0 blocks ==16564== possibly lost: 0 bytes in 0 blocks ==16564== still reachable: 4 bytes in 1 blocks ==16564== suppressed: 0 bytes in 0 blocks ... ... Segmentation fault
不断学习中。。。
时间: 2024-10-03 21:54:10