Valgrind安装

参考

  1. valgrind基本功能介绍、基础使用方法说明
  2. Ubuntu20.04安装valgrind

安装步骤

首先进入一个自定义的文件夹

例如:cd ~/valgrind/

  1. 下载安装包
  2. 解压缩
    tar -xf valgrind-xxx.tar.bz2   # xxx -=> 版本号 
    
  3. make
    A.
    sudo apt-get install automake
    
    B.
    sudo apt-get install autoconf
    
    C.
    cd valgrind-xxx
    
    D.
    ./autogen.sh
    
    E.
    ./configure
    
    F.
    make -jn # n: 并行数,依据自己的cpu核心判断,2,4,8 足够
    
    G.
    sudo make install 
    

测试

安装是否成功

valgrind --version
# 输出 valgrind-3.22.0

是否正常使用

  1. 写个错误的c++程序
//test.cpp
#include <iostream>
using namespace std;
// 有越界行为
int main()
{
	int *a = new int[10];
	cout << "ook" << endl;
	a[9] = 10;
	a[10] = 11;
	return 0;
}
  1. 编译
  • //-g:让 memcheck 工具可以取到出错的具体行号
    g++ test.cpp -g -o test 
    valgrind --tool=memcheck --log-file=test.log --leak-check=yes  ./test
    
  1. 查看 .log 文件
    cat test.log
    
    • 如果报错
    valgrind:  Note that if you are debugging a 32 bit process on a
    valgrind:  64 bit system, you will need a corresponding 32 bit debuginfo
    valgrind:  package (e.g. libc6-dbg:i386).
    valgrind:  Cannot continue -- exiting now.  Sorry.
    
    请安装sudo apt-get install libc6-dbg
    • 正常
      ==35072== Memcheck, a memory error detector
      ==35072== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
      ==35072== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
      ==35072== Command: ./test1_g
      ==35072== Parent PID: 4122
      ==35072==
      ==35072== Invalid write of size 4
      ==35072==    at 0x109224: main (test1.cpp:10)
      ==35072==  Address 0x4ddaca8 is 0 bytes after a block of size 40 alloc'd
      ==35072==    at 0x484A703: operator new[](unsigned long) (vg_replace_malloc.c:725)
      ==35072==    by 0x1091DE: main (test1.cpp:7)
      ==35072==
      ==35072==
      ==35072== HEAP SUMMARY:
      ==35072==     in use at exit: 40 bytes in 1 blocks
      ==35072==   total heap usage: 3 allocs, 2 frees, 73,768 bytes allocated
      ==35072==
      ==35072== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
      ==35072==    at 0x484A703: operator new[](unsigned long) (vg_replace_malloc.c:725)
      ==35072==    by 0x1091DE: main (test1.cpp:7)
      ==35072==
      ==35072== LEAK SUMMARY:
      ==35072==    definitely lost: 40 bytes in 1 blocks
      ==35072==    indirectly lost: 0 bytes in 0 blocks
      ==35072==      possibly lost: 0 bytes in 0 blocks
      ==35072==    still reachable: 0 bytes in 0 blocks
      ==35072==         suppressed: 0 bytes in 0 blocks
      ==35072==
      ==35072== For lists of detected and suppressed errors, rerun with: -s
      ==35072== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
      
      可以查看第一篇参考文献看具体含义

感谢

忠心感谢参考文章的作者!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值