下载
http://valgrind.org/downloads/current.html
解压
编译
./configure make sudo make install
测试
#include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char **argv) { //char array[32] = {0}; char* buffer = NULL; buffer = (char*)malloc(32); //崩溃 buffer[56] = 0; free(buffer); return 0; }
编译运行
gcc -g test.c valgrind --tool=memcheck --leak-check=full ./a.out
显示如下:
==13794== Memcheck, a memory error detector ==13794== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==13794== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info ==13794== Command: ./a.out ==13794== ==13794== Invalid write of size 1 ==13794== at 0x400593: main (test.c:11) ==13794== Address 0x5205078 is 24 bytes after a block of size 32 in arena "client" ==13794== ==13794== ==13794== HEAP SUMMARY: ==13794== in use at exit: 0 bytes in 0 blocks ==13794== total heap usage: 1 allocs, 1 frees, 32 bytes allocated ==13794== ==13794== All heap blocks were freed -- no leaks are possible ==13794== ==13794== For counts of detected and suppressed errors, rerun with: -v ==13794== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
注意其中:
==13794== Invalid write of size 1 ==13794== at 0x400593: main (test.c:11)
实用性探讨
需要说明的是,对于类似char array[32] = {0};,提示上就要差一些
==13772== Invalid read of size 1 ==13772== at 0x4E5B800: (below main) (libc-start.c:285) ==13772== Address 0x0 is not stack'd, malloc'd or (recently) free'd
具体情况,以后通过试用再做补充.