使用Valgrind查找内存泄漏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a909204013/article/details/85028832

在网上找了一个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

猜你喜欢

转载自blog.csdn.net/a909204013/article/details/85028832