点击(此处)折叠或打开
- #include<stdio.h>
- #include<stdlib.h>
- #include<mcheck.h>
- #include<assert.h>
- int main()
- {
- assert(!setenv("MALLOC_TRACE","./malloc.log",1)) ;
- mtrace();
- int *p = malloc(100*sizeof(int));
- return 0;
- }
- setenv("MALLOC_TRACE","./malloc.log",1)
- export MALLOC_TRACE=~/program/C/MEM_CHECK/memcheck.log
点击(此处)折叠或打开
- root@libin:~/program/C/mem_bug# gcc -o test test.c -g
- root@libin:~/program/C/mem_bug# ./test
- root@libin:~/program/C/mem_bug# ll
- 总用量 28
- drwxr-xr-x 2 root root 4096 2012-04-03 12:17 ./
- drwxr-xr-x 36 root root 4096 2012-04-03 12:01 ../
- -rw-r--r-- 1 root root 155 2012-04-03 12:17 malloc.log
- -rwxr-xr-x 1 root root 8550 2012-04-03 12:17 test*
- -rw-r--r-- 1 root root 208 2012-04-03 12:17 test.c
- root@libin:~/program/C/mem_bug# mtrace ./test malloc.log
- - 0x09a68008 Free 3 was never alloc'd 0x1da8ef
- - 0x09a68028 Free 4 was never alloc'd 0x1da8f7
- Memory not freed:
- -----------------
- Address Size Caller
- 0x09a683b0 0x190 at /home/libin/program/C/mem_bug/test.c:11