valgrind结果查看
2011年12月30日
程序示例:test3.cpp
1 #include
2 #include
3
4 void f(void)
5 {
6 int *x =(int *) malloc(10*sizeof(int));
7 x[10] = 0;
8 }
9
10 int main()
11 {
12 f();
13 return 0;
14 }
编译链接该程序:
[root@p12 wangjia]# g++ -o test3 -ggdb test3.cpp
使用valgrind检查内存的调试信息:
[root@p12 wangjia]# valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./test3
==2866== Memcheck, a memory error detector.
==2866== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==2866== Using LibVEX rev 1606, a library for dynamic binary translation.
==2866== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==2866== Using valgrind-3.2.0, a dynamic binary instrumentation framework.
==2866== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==2866== For more details, rerun with: -v
==2866==
==2866== Invalid write of size 4
==2866== at 0x80483BF: f() (test3.cpp:7) //错误一:数组越界 + 错误位置
==2866== by 0x80483E8: main (test3.cpp:12)
==2866== Address 0x4252050 is 0 bytes after a block of size 40 alloc'd
==2866== at 0x401A6D6: malloc (vg_replace_malloc.c:149)
==2866== by 0x80483B5: f() (test3.cpp:6)
==2866== by 0x80483E8: main (test3.cpp:12)
==2866==
==2866== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 18 from 1)
==2866== malloc/free: in use at exit: 40 bytes in 1 blocks. //malloc 和 free 的使用情况
==2866== malloc/free: 1 allocs, 0 frees, 40 bytes allocated. //错误二:没有释放
==2866== For counts of detected errors, rerun with: -v
==2866== searching for pointers to 1 not-freed blocks.
==2866== checked 104,552 bytes.
==2866==
==2866==
==2866== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2866== at 0x401A6D6: malloc (vg_replace_malloc.c:149)
==2866== by 0x80483B5: f() (test3.cpp:6) //错误二所在位置
==2866== by 0x80483E8: main (test3.cpp:12)
==2866==
==2866== LEAK SUMMARY:
==2866== definitely lost: 40 bytes in 1 blocks.
==2866== possibly lost: 0 bytes in 0 blocks.
==2866== still reachable: 0 bytes in 0 blocks.
==2866== suppressed: 0 bytes in 0 blocks.
下面介绍下Valgrind:
Valgrind是 x86架构Linux上的多重用途代码剖析和内存调试工具。但它的主要功能还是对内存的调试,而且它的默认工具也是启动 memcheck。你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的malloc和free或者 C++中的new和 delete。使用Valgrind的工具包,你可以自动的检测许多内存管理和线程的bug,避免花费太多的时间在bug寻找上,使得你的程序更加稳固。
一、Valgrind的主要功能(结合memcheck工具)
1,使用未初始化的内存 (Use of uninitialised memory)
2,使用已经释放了的内存 (Reading/writing memory after it has been free'd)
3,使用超过 malloc分配的内存空间(Reading/writing off the end of malloc'd blocks)
4,对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
5,申请的空间是否有释放 (Memory leaks -- where pointers to malloc'd blocks are lost forever)
6, malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
7, src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
二、Valgrind下载与安装
下载地址:http://download.chinaunix.net/download/0011000/10928.shtml
安装:
1)解压valgrind-3.2.0.tar.bz2
$bunzip2 valgrind-3.2.0.tar.bz2
$tar vfx valgrind-3.2.0.tar
2)解压后生成一个 valgrind-3.2.0目录
$cd valgrind-3.2.0
3)编译安装valgrind
$./configure
$make && make install
到这里valgrind就编译安装完成。
三、Valgrind使用参数
内存泄漏是最难发现的常见错误之一,因为除非用完内存或调用 malloc失败,否则都不会导致任何问题。实际上,使用像C 或C++这类没有垃圾回收机制的语言时,你一大半的时间都花费在处理如何正确释放内存上。如果程序运行时间足够长,一个小小的失误也会对程序造成重大的影响。
Valgrind支持很多工具 :memcheck,addrcheck, cachegrind,massif,helgrind 和callgrind等。在运行Valgrind 时,你必须指明想用的工具。如$valgrind
发表评论
-
busybox 详解(一篇讲busybox很详细的文章)
2012-01-20 12:19 769busybox 详解(一篇讲busybox ... -
so库的性能注意事项
2012-01-20 12:18 823so库的性能注意事项 2010年09月07日 一个共享库 ... -
Sql server 日记 (孤立用户)
2012-01-20 12:18 766Sql server 日记 (孤立用户) 2010年07月2 ... -
Python 数据库接口模块
2012-01-19 17:00 961Python 数据库接口模块 2 ... -
Python 3.2.2 RC1发布
2012-01-19 17:00 747Python 3.2.2 RC1发布 2011年08月16日 ... -
Python 与 C++ 的交互编程
2012-01-19 17:00 1091Python 与 C++ 的交互编程 2011年03月28日 ... -
python教程入门《3》
2012-01-19 17:00 738python教程入门《3》 2011年02月13日 第六 ... -
用Python处理邮件
2012-01-19 17:00 677用Python处理邮件 2011年0 ... -
social game 前端思考
2012-01-17 06:45 709social game 前端思考 2011 ... -
Flash&Flex大全
2012-01-17 06:45 622Flash&Flex大全 2011年02月24日 ... -
笑到肚子疼
2012-01-17 06:44 465笑到肚子疼 2011年11月02日 1 -
剪色坊北京直营店(呼市)--死人的一次烫发经历----剪色坊北京直营店(呼市)
2012-01-17 06:44 681剪色坊北京直营店(呼市)--死人的一次烫发经历----剪色坊北 ... -
QT--QSocketNotifier类介绍
2012-01-16 05:34 976QT--QSocketNotifier类介绍 2011年12 ... -
00000000000000000
2012-01-16 05:34 72100000000000000000 2012年01月06日 ... -
IPhone 开发经验教训总结 -- 仅供参考
2012-01-16 05:34 635IPhone 开发经验教训总结 ... -
IP组播技术综述二
2012-01-16 05:34 649IP组播技术综述二 2011 ...
相关推荐
valgrind是一款运行在linux下的,用来定位c/c++程序中内存使用方面的错误的工具,包括:内存泄漏、使用未初始化的内存、读/写已释放的内存、读/写内存越界、使用malloc/new/new[]和free/delete/delete[]不匹配,等等...
valgrind-arm64.zip 预编译好的valgrind for android aarch64。 可参考下面链接跳过编译部分,直接安装使用。 https://blog.csdn.net/yaxf999/article/details/53749184 valgrind内存检测工具使用方法: adb push .\...
valgrind
内存检查工具valgrind 的使用方法,通过valgrind 可以检查程序中的内存情况。
valgrind安装包 安装步骤 安装说明;以及一些检查内存泄漏的用法
亲测可以正常使用的valgrind在android板子上使用,对于需要用valgrind检测内存泄漏等问题的开发者有参考价值。
Valgrind的简介和使用方法,介绍了Valgrind的使用以及如何进行程序检查
valgrind 2017年版的最新手册manual. 并且带有书签. 阅读文档比博客收获更多.
Valgrind是一款很好用的C/C++测试工具,可以有效的测试其中的内存分配及泄露情况,在Linux环境使用简单
预编译好的valgrind for android armeabi-v7a。 可参考下面链接跳过编译部分,直接安装使用。 https://blog.csdn.net/yaxf999/article/details/53749184 adb push valgrind-arm/Inst\data/local/Inst /data/local...
valgrind,内存检查工具,用于内存泄漏的检查
Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。Valgrind这个名字取自北欧神话中英灵殿的入口。 Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届...
valgrind安装与使用
valgrind arm64交叉编译包,可以直接运行,里面有安装说明。 附带解决valgrind运行时报错“Note that if you are debugging a 32 bit process on a 64 bit system...”的依赖库:dpkg -i libc6_2.31-13+deb11u5_arm...
valgrind-3.19.0 内存检测工具 aarch64 6.5.0交叉编译移植版本
Valgrind tool for cross compilation on armv6.
valgrind_manual.pdf
压缩包为valgrind-2.13.0源码,需要的同学自行下载,也可以到官网下载,官网可能会有更新的版本。
valgrind最新版最新版下载,所以提供已经下载好的最新valgrind
valgrind结构图,描述了Valgrind的基本结构,有助于了解Valgrind的基本原理