valgrind安装,使用

一、下载软件

1、下载官网

http://valgrind.org/downloads/current.html

2、执行命令:

[root@test valgrind]# wget https://sourceware.org/pub/valgrind/valgrind-3.15.0.tar.bz2

等进度条走完就下载好了。

二、安装

1、先解包

[root@test valgrind]# tar -xvf valgrind-3.15.0.tar.bz2

2、进文件夹

[root@test valgrind]# cd valgrind-3.15.0/

3、执行文件,注意有没有执行权限

[root@test valgrind-3.15.0]# ./configure

4、编译,安装

[root@test valgrind-3.15.0]# make

[root@test valgrind-3.15.0]# make install

5、验证安装是否成功

[root@test valgrind-3.15.0]# valgrind --version
valgrind-3.15.0

能正确输出版本号,就是安装成功了。

三、测试代码

1、官网的快速入门教程

http://valgrind.org/docs/manual/QuickStart.html

2、编写测试代码

按照

http://valgrind.org/docs/manual/quick-start.html#quick-start.intro

上的例子,来编写第一个测试例子。

#include <stdlib.h>

  void f(void)
  {
     int* x = malloc(10 * sizeof(int));
     x[10] = 0;        // problem 1: heap block overrun
  }                    // problem 2: memory leak -- x not freed

  int main(void)
  {
     f();
     return 0;
  }

创建main.cpp文件

[root@test valgrind]# vim main.c

把上面的代码黏贴进去,然后编译一下。

[root@test valgrind]# gcc -Wall main.c -g -o main

得到可执行的main程序

3、检测程序bug

用valgrind来检查程序

[root@test valgrind]# valgrind --tool=memcheck --leak-check=full ./main

得到输出

==11394== Memcheck, a memory error detector
==11394== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11394== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==11394== Command: ./main
==11394==
==11394== Invalid write of size 4
==11394==    at 0x400500: f (main.c:6)
==11394==    by 0x400511: main (main.c:11)
==11394==  Address 0x5204068 is 0 bytes after a block of size 40 alloc'd
==11394==    at 0x4C29E03: malloc (vg_replace_malloc.c:309)
==11394==    by 0x4004F3: f (main.c:5)
==11394==    by 0x400511: main (main.c:11)
==11394==
==11394==
==11394== HEAP SUMMARY:
==11394==     in use at exit: 40 bytes in 1 blocks
==11394==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==11394==
==11394== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==11394==    at 0x4C29E03: malloc (vg_replace_malloc.c:309)
==11394==    by 0x4004F3: f (main.c:5)
==11394==    by 0x400511: main (main.c:11)
==11394==
==11394== LEAK SUMMARY:
==11394==    definitely lost: 40 bytes in 1 blocks
==11394==    indirectly lost: 0 bytes in 0 blocks
==11394==      possibly lost: 0 bytes in 0 blocks
==11394==    still reachable: 0 bytes in 0 blocks
==11394==         suppressed: 0 bytes in 0 blocks
==11394==
==11394== For lists of detected and suppressed errors, rerun with: -s
==11394== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

4、分析检测结果,修改代码

从结果的最后一行,我们可以看出,一共有2个错误:

==11394== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

一个是数组越界,根据行数可以直接找到错误在第6行。

==11394== Invalid write of size 4
==11394==    at 0x400500: f (main.c:6)

第二个是内存没有释放

==11394== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==11394==    at 0x4C29E03: malloc (vg_replace_malloc.c:309)
==11394==    by 0x4004F3: f (main.c:5)

在第5行,malloc之后没有free。

修改之后的代码如下所示

#include <stdlib.h>

  void f(void)
  {
     int* x = malloc(10 * sizeof(int));
     x[9] = 0;        // problem 1: heap block overrun
     free(x);
  }                    // problem 2: memory leak -- x not freed

  int main(void)
  {
     f();
     return 0;
  }

5、检测新代码

重新编译,检测新代码

[root@test valgrind]# gcc -Wall main.c -g -o main
[root@test valgrind]# valgrind --tool=memcheck --leak-check=full ./main
==11881== Memcheck, a memory error detector
==11881== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11881== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==11881== Command: ./main
==11881==
==11881==
==11881== HEAP SUMMARY:
==11881==     in use at exit: 0 bytes in 0 blocks
==11881==   total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==11881==
==11881== All heap blocks were freed -- no leaks are possible
==11881==
==11881== For lists of detected and suppressed errors, rerun with: -s
==11881== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

可以看到,这次已经没有错误了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值