From: http://bbs.chinaunix.net/thread-908769-1-1.html

如题。

-------------------------------

找到了!  http://www.valgrind.org/

同时附一个转自 http://blog.chinaunix.net/u/18381/showart.php?id=162015 的示例:

#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;
}

编译代码:  
gcc -Wall example.c -g -o example

注意:gcc 的-g 选项让Valgrind调试输出时指出相应信息的代码所在的行号。

运行valgrind: 
valgrind --tool=memcheck --leak-check=yes ./example

valgrind的执行结果: 
==6742== Memcheck, a memory error detector for x86-linux.
==6742== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==6742== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==6742== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==6742== For more details, rerun with: -v
==6742==
==6742== Invalid write of size 4
==6742==    at 0x8048384: f (example.c:6)
==6742==    by 0x80483AC: main (example.c:12)
==6742==  Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)
==6742==
==6742== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
==6742== malloc/free: in use at exit: 40 bytes in 1 blocks.
==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==6742== For counts of detected errors, rerun with: -v
==6742== searching for pointers to 1 not-freed blocks.
==6742== checked 1360800 bytes.
==6742==
==6742==
==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)
==6742==
==6742== LEAK SUMMARY:
==6742==    definitely lost: 40 bytes in 1 blocks.
==6742==    possibly lost:   0 bytes in 0 blocks.
==6742==    still reachable: 0 bytes in 0 blocks.
==6742==         suppressed: 0 bytes in 0 blocks.
==6742== Reachable blocks (those to which a pointer was found) are not shown.
==6742== To see them, rerun with: --show-reachable=yes

上面的C程序存在两个错误:1. 数组下标越界;2. 分配的内存没有释放,存在内存泄露的问题。
对于错误1,看Valgrind的调试信息片断
==6742== Invalid write of size 4
==6742==    at 0x8048384: f (example.c:6)
==6742==    by 0x80483AC: main (example.c:12)
==6742==  Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)

对于错误2,看这个
==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
......
==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)

在Linux下编写C程序,怎么检查程序是否有内存泄漏?相关推荐

  1. linux 编写完程序吗,linux下编写C++程序

    注:本文写的内容全部在ubuntu12.04下完成. 要在linux下写C++程序,要懂的一点编译的知识.下面介绍一下. 源代码->预处理器(负责将代码补充)->汇编程序(生成汇编语言)- ...

  2. 在Linux下编写C++程序,使用gcc编译c++出错

    问题 /usr/bin/ld: /tmp/ccMSWoFl.o: in function __static_initialization_and_destruction_0(int, int)': f ...

  3. linux 程序播放音频,linux下编写C++程序播放音频

    linq实现数组转符号分割的字符串(备忘) fitemidstr = string.Join(",", detailpre1.Select(i => i.Key.ToStri ...

  4. Linux运行911,韦东山-Linux下编写GT911触摸驱动 - 百问网嵌入式问答社区

    源码在最下面 问题一:资源获取Gt911数据手册 在韦老师给的资料里,路径为\06_Datasheet\Extend_modules\7寸LCD模块\电容触控芯片GT911 Datasheet_121 ...

  5. linux应用程序的编写实验原理,操作系统实验 1.在linux下编写一个应用程序 联合开发网 - pudn.com...

    操作系统实验 所属分类:Linux/Unix编程 开发工具:C/C++ 文件大小:1KB 下载次数:3 上传日期:2019-05-01 20:34:21 上 传 者:烟雨南风起 说明:  1.在lin ...

  6. Linux下C语言程序编写及执行和分步骤编译链接C源代码

    实验一:Linux下C语言程序编写及执行 //hello.c #include <stdio.h> int main(){int x = 1;int y = 2;int sum = x + ...

  7. Linux 下几款程序内存泄漏检查工具

    Linux 下几款程序内存泄漏检查工具 chenyoubing | 发布于 2016-07-23 10:08:09 | 阅读量 93 | 无 写这篇博客的原因呢是因为自己在编写基于Nginx磁盘缓存管 ...

  8. 在Linux下编写Daemon

    在Linux下编写Daemon 转自:http://blog.163.com/prevBlogPerma.do?host=manyhappy163&srl=164476831201071811 ...

  9. linux下软件编译终止,[2018年最新整理]linux下编译运行程序命令大全.ppt

    [2018年最新整理]linux下编译运行程序命令大全 1. 项目课题引入 2. Vi编辑器的使用方法 3. Linux中C语言程序的编辑 4. Linux中C语言程序的运行 5. 现场演示案例 课题 ...

最新文章

  1. mongodb java 日志分析_记一次log4j与mongodb集成引发的问题分析
  2. hdu2056 矩形重叠面积(水题)
  3. 中国SaaS死或生之五:目标、方法、钱,一个都不能少
  4. java 数组练习题,Java练习题 - 数组01
  5. python网络通信框架_Python运维-Socket网络编程 (1)
  6. ehchache验证缓存过期的api_Ehcache缓存时间设置
  7. Amazon上最畅销的「操作系统书」有哪些?
  8. java集合set的使用_浅析Java中的set集合类型及其接口的用法
  9. 入职阿里啦!docker-e命令参数
  10. php oracle817,Oracle 817 For Linux/Unix安装文档-数据库专栏,ORACLE
  11. merlin.acs的使用方法 merlin.acs添加右键菜单
  12. HBase 学习(三) JavaAPI的使用
  13. ROS 2正式版终于来了,还增加了这些新特性
  14. cad多段线画圆弧方向_【学员分享】CAD多段线用法
  15. 毕业论文的页眉、页脚奇偶页不同的设置方法
  16. 优达(Udacity)customer_segments
  17. 动态规划(DP)经典模型
  18. 该充电时就充电,电池生命力才会持久,人何尝不是呢?
  19. 微信公众号前后端分离授权登录
  20. 科技云报道:华为云数据灾备,守护企业的生命线

热门文章

  1. 在FAANG面试中破解堆算法
  2. 数据可视化机器学习工具在线_为什么您不能跳过学习数据可视化
  3. 2027. 转换字符串的最少操作次数
  4. leetcode1386. 安排电影院座位(贪心)
  5. leetcode1415. 长度为 n 的开心字符串中字典序第 k 小的字符串(回溯)
  6. leetcode89. 格雷编码
  7. 如何使用Node.js,Express和MongoDB设置GraphQL服务器
  8. 沙箱模式以及其使用到的IIFE
  9. 用Vue.js开发一个电影App的前端界面
  10. 学习flex布局(弹性布局)