Electric Fence (efence) stops your program on theexact instruction that overruns (or underruns) a malloc() memorybuffer. GDB will then display the source-code line that causes thebug. It works by using the virtual-memory hardware to create ared-zone at the
border of each buffer - touch that, and your programstops. Catch all of those formerly impossible-to-catch overrun bugsthat have been bothering you for years.
efence对于调试动态内存的错误很有效。下面是efence库的安装:
首先,下载源文件 :
点击打开链接
然后,用命令 tar -zxvf electric-fence-2.1.13.tar.gz 解压文件包;
然后,进入electric-fence-2.1.13文件夹,编辑Makefile第九行,根据自己系统man目录的位置,修改为MAN_INSTALL_DIR= /usr/share/man/man3
注:1.Makefile应该不改也可以
2. 我的系统是openSUSE, 如果在你的系统上可以直接找到/usr/man/man3,那就不用改Makefile了。
最后是普通的安装源代码过程:
make all
sudo make install
make clean
efence库的链接:
常用的格式就是在命令行的最后加上 -lefence -lpthread,比如:
- #include <stdio.h>
- #include <malloc.h>
- int main(void)
- {
- int *a = (int*)malloc(2*sizeof(int));
- for (int i=0;i<=2;i++) {
- a[i] = i;
- printf("%d\n", a[i]);
- }
- free(a);
- return 0;
- }
编译: cc -g3 -Wall -o out_of_bound out_of_bound.c -lefence -lpthread
运行: ./out_of_bound ,看输出结果就知道efence的威力了。
注:efence库依赖于pthread(多线程)库
参考资料: 软件调试的艺术P188, man-page,stackoverflow