Linux检测C代码是否内存泄漏

安装valgrind

下载安装包并安装

链接:https://pan.baidu.com/s/1M3VyIu1F27UuSVKgwOtL_A?pwd=b5ol
提取码:b5ol

tar -xjvf valgrind-3.11.0.tar.bz2
cd valgrind-3.11.0/
./configure
make
sudo make install

检查是否安装成功

valgrind --version

返回:valgrind-3.11.0

使用valgrind检查是否存在内存泄漏

编译要检测的C代码

gcc -g test.c -o test

检测C代码是否正常运行

./test

使用valgrind检测

valgrind --leak-check=yes ./test
valgrind --leak-check=yes ./test
==6067== Memcheck, a memory error detector
==6067== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6067== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==6067== Command: ./sigcheck
==6067== 
vex amd64->IR: unhandled instruction bytes: 0xF3 0xF 0x1E 0xFA 0x55 0x48 0x89 0xE5
vex amd64->IR:   REX=0 REX.W=0 REX.R=0 REX.X=0 REX.B=0
vex amd64->IR:   VEX=0 VEX.L=0 VEX.nVVVV=0x0 ESC=0F
vex amd64->IR:   PFX.66=0 PFX.F2=0 PFX.F3=1
==6067== valgrind: Unrecognised instruction at address 0x4021050.
==6067==    at 0x4021050: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
==6067==    by 0x40202B7: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
==6067== Your program just tried to execute an instruction that Valgrind
==6067== did not recognise.  There are two possible reasons for this.
==6067== 1. Your program has a bug and erroneously jumped to a non-code
==6067==    location.  If you are running Memcheck and you just saw a
==6067==    warning about a bad jump, it's probably your program's fault.
==6067== 2. The instruction is legitimate but Valgrind doesn't handle it,
==6067==    i.e. it's Valgrind's fault.  If you think this is the case or
==6067==    you are not sure, please let us know and we'll try to fix it.
==6067== Either way, Valgrind will now raise a SIGILL signal which will
==6067== probably kill your program.
==6067== 
==6067== Process terminating with default action of signal 4 (SIGILL)
==6067==  Illegal opcode at address 0x4021050
==6067==    at 0x4021050: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
==6067==    by 0x40202B7: ??? (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
==6067== 
==6067== HEAP SUMMARY:
==6067==     in use at exit: 0 bytes in 0 blocks
==6067==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==6067== 
==6067== All heap blocks were freed -- no leaks are possible
==6067== 
==6067== For counts of detected and suppressed errors, rerun with: -v
==6067== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Illegal instruction (core dumped)

我觉得no leaks are possible这个就说明没有泄漏。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值