使用Valgrind查找内存泄漏

在网上找了一个C语言版本的base64代码,编译通过,不过运行的时候报了corrupted size vs. prev_size错误

在这里插入图片描述
网上查了一下资料,大致说是内存泄漏。但是怎么分析哪儿泄漏,为什么泄漏? 在网上找到一款神器Valgrind 。专用于分析内存泄漏等各种疑难杂症。

1、安装

官网下载,解压,按照readme进行操作

To install from a tar.bz2 distribution:

  4. Run ./configure, with some options if you wish.  The only interesting
     one is the usual --prefix=/where/you/want/it/installed.

  5. Run "make".

  6. Run "make install", possibly as root if the destination permissions
     require that.

  7. See if it works.  Try "valgrind ls -l".  Either this works, or it
     bombs out with some complaint.  In that case, please let us know
     (see www.valgrind.org).

2、编译文件

gcc test.c -g,加上-g 选项,会保留代码的文字信息,便于调试。最明显的区别就是-g后会有错误代码的行数形式,直接编译为可执行文件没有行数,不好确定问题所在。

3、valgrind调试

valgrind ./a.out即可查看程序状况

在这里插入图片描述

从信息中可以看到,该代码总共有四处越界。可以在越界处打印下标值,可以看到具体的越界值来修改malloc空间大小。

参考网站:
https://epitech-2022-technical-documentation.readthedocs.io/en/latest/valgrind.html 很好的入门教程

https://www.linuxidc.com/Linux/2012-06/63754.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值