Valgrind内存读写越界检测工具使用说明

valgrind这款工具是运行在linux下的,可以用来定位c/c++程序中内存使用方面的错误。包括:内存泄漏、使用未初始化的内存、读/写已释放的内存、读/写内存越界、使用malloc/new/new[]和free/delete/delete[]不匹配,等等。
valgrind官方网站: http://valgrind.org/,是一款open source软件。我下载的时候最新版是3.2.3。安装过程很简单——configure,make,make install,安装后不需要任何设置,直接可以开始使用。
valgrind运行方法很简单:valgrind --leak-check=full --track-fds=yes 程序 参数

  这样你的程序就会在valgrind监控下运行了,结果会直接输出到屏幕上。如果想把结果输出到日志文件(通常也需要这样),用如下的命令:valgrind --log-file=valgrind_log --leak-check=full --track-fds=yes 程序 参数


运行 :valgrind --leak-check=full --track-fds=yes test  -a 后可以得到如下结果。

Address 0x13cd0c34 is 0 bytes after a block of size 28 alloc'd

检查时发现上面打印出的语句,说明内存分配后全部空间都被占用,这是请注意字符串拷贝可能越界。

Valgrind is an instrumentation framework for building dynamic analysis tools. Thereare Valgrind tools that can automatically detect many memory managementand threading bugs, and profile your programs in detail. You can alsouse Valgrind to build new tools.

The Valgrind distribution currently includes six production-qualitytools: a memory error detector, two thread error detectors, a cacheand branch-prediction profiler, a call-graph generating cache andbranch-prediction profiler, and a heap profiler. It also includesthree experimental tools: a heap/stack/global array overrun detector,a second heap profiler that examines how heap blocks are used, and aSimPoint basic block vector generator. It runs on the followingplatforms: X86/Linux, AMD64/Linux, ARM/Linux, PPC32/Linux,PPC64/Linux, S390X/Linux, MIPS/Linux, ARM/Android (2.3.x and later),X86/Android (4.0 and later), X86/Darwin and AMD64/Darwin (Mac OS X10.6 and 10.7, with limited support for 10.8).

Valgrind is Open Source /Free Software,and is freely available under theGNU General Public License, version 2.


你可能感兴趣的:(Valgrind内存读写越界检测工具使用说明)