内存检测工具Valgrind
Valgrind可以检测如下的内存问题:
1,使用未初始化的内存;
2,使用已经释放的内存;
3,使用超过malloc分配的内存空间;
4,对堆栈的非法访问;
5,内存泄露;
6,mallock/free/new/delete非匹配的使用内存申请和释放函数;
7,使用memcpy等函数源地址和目的地址重叠错误。
选项看这里:
举个例子:
//example1.c
int main(void) {
int i[3];
if(i[0] == 1)
i[1] = 0;
return 0;
}
编译后
[xchunhua@localhost Exp]$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./example1
==3099== Memcheck, a memory error detector.
==3099== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==3099== Using LibVEX rev 1732, a library for dynamic binary translation.
==3099== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==3099== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==3099== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==3099== For more details, rerun with: -v
==3099==
==3099==
==3099== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 1)
==3099== malloc/free: in use at exit: 0 bytes in 0 blocks.
==3099== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==3099== For counts of detected errors, rerun with: -v
==3099== All heap blocks were freed -- no leaks are possible.
阅读(856) | 评论(0) | 转发(0) |