memwatch下载:http://www.linkdata.se/sourcecode.html
特点:简单易用,无需安装,能检测未被释放的内存
memwatch使用举例
memwatch不需要安装,只要下载包解压即可,有用的文件只有memwatch.c&memwatch.h,把这两个文件放入要检测的程序的文件夹中即可。编译的命令为:gcc -DMEMWATCH -DMW_STDIO test.c memwatch.c -otest
1 在需要测试的代码里添加#include"memwatch.h"
需要测试的代码如下test.c
#include<stdlib.h>
#include"memwatch.h"
void f(void)
{
int*x=malloc(10*sizeof(int));
x[10]=0;
}
int main(void)
{
f();
return 0;
}
3 运行./test
4 运行结果会生成一个memwatch.log
memwatch.log的內容如下:
============= MEMWATCH 2.71Copyright (C) 1992-1999 Johan Lindh =============Started at Wed Aug 3 17:10:46 2011
Modes: __STDC__ 64-bit mwDWORD==(unsigned int)
mwROUNDALLOC==8 sizeof(mwData)==56 mwDataSize==56
Stopped at Wed Aug 3 17:10:46 2011
unfreed: <1> test1.c(5), 40 bytes at0x57f3310[overflowed]{FE FE FE FE FE FE FE FE FE FE FE FE FE FE FE FE................}
Memory usage statistics (global):
N)umber of allocations made: 1
L)argest memoryusage: 40
T)otal of all alloc() calls: 40
U)nfreed bytestotals: 40