安装编译步骤:
wget http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2
tar xvf valgrind-3.12.0.tar.bz2
cd valgrind-3.12.0
sudo apt-get install automake
./autogen.sh
./configure --prefix=/<pathto>/valgrind-3.12.0
make
make install
添加至环境变量:
sudo gedit ~/.bashrc
添加:
export PATH=$PATH:/<pathto>/valgrind-3.12.0/bin
export VALGRIND_LIB="/<pathto>/valgrind-3.12.0/lib/valgrind"
source .bashrc
检测是否成功编译:
valgrind --version
测试用例:
#include <stdlib.h>
int main()
{
int * pArray =malloc(sizeof(int) * 5);
pArray[5] = 1;
int * pInt ;
int x = *pInt;
* pInt = 2;
return 0;
}
编译:gcc -g -o test main.c
检测:valgrind --leak-check=yes ./test
检测32位程序的时候需要安装对应的i386库否则会提示错误:
==3062== Memcheck, a memory error detector
==3062== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3062== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==3062== Command: ./test
==3062==
valgrind: Fatal error at startup: a function redirection
valgrind: which is mandatory for this platform-tool combination
valgrind: cannot be set up. Details