valgrind结果查看

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 }
  编译链接该程序:
  # g++ -o test3 -ggdb test3.cpp
  使用valgrind检查内存的调试信息:
  # 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.
  [b]下面介绍下Valgrind:
  [/b]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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值