linux 使用valgrind检查内存使用问题

valgrind运行错误

 http://www.oschina.net/translate/valgrind-memcheck?cmp

  • 问题描述
    • valgrind运行时,无法找到相关工具文件,具体报错如下
    • valgrind: failed to start tool 'memcheck' for platform 'amd64-linux': No such file or directory
  • 解决方法
    •  导出VALGRIND_LIB路径,用法如下(假设valgrind已经被安装到/home/test/valgrind目录):
      • export VALGRIND_LIB=/home/test/valgrind/lib/valgrind
    • 或者也可以如下方式调用valgrind:
      • VALGRIND_LIB=/home/test/valgrind/lib/valgrind /home/test/valgrind/bin/valgrind --help

  • 介绍
  • 安装
  • 使用
    • 使用未初始化内存
    • 越界使用内存
    • 内存泄漏
    • 两次释放内存
    • 使用已经释放的内存
    • 不匹配使用newdelete或newdelete

介绍

Valgrind是用于内存检泄漏检测、内存调试以及性能分析的工具集。这里主要介绍其用与内存相关的工具Memcheck,它可以发现大部分的内存问题,例如内存泄漏,使用未初始化内存、内存越界等问题。

安装

我用的时Ubuntu,首先安装Valgrind工具:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> apt-get install valgrind</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

安装完成后,可以查看是否安装成功:

<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">valgrind</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">help</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

使用

Valgrind使用比较简单:

<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">valgrind --tool</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">memcheck Path ./excuteFile</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

注意,在编译为可执行文件时,最好加上-g选项,这样可以看到程序内存问题出现的语句;加上-O0选项,不让编译器给程序做优化。

使用未初始化内存

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#include <iostream></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>* p;
    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*p</span>=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

编译

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">g<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">++</span> test1<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>cpp <span class="hljs-attribute" style="box-sizing: border-box;">-g</span> <span class="hljs-attribute" style="box-sizing: border-box;">-O0</span>  <span class="hljs-attribute" style="box-sizing: border-box;">-o</span> test1</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

使用Valgrind查看

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ valgrind --tool=memcheck ./test1 
=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">Memcheck</span>, a memory error detector
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">Copyright</span> (<span class="hljs-constant" style="box-sizing: border-box;">C</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2002</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2013</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">GNU</span> <span class="hljs-constant" style="box-sizing: border-box;">GPL</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d, by Julian Seward et al.
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">Using</span> <span class="hljs-constant" style="box-sizing: border-box;">Valgrind</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.10</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>.<span class="hljs-constant" style="box-sizing: border-box;">SVN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">LibVEX</span>; rerun with -h <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> copyright info
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">Command</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> ./test1
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">Use</span> of uninitialised value of size <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4006D5</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test1.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">Invalid</span> write of size <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4006D5</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test1.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==  <span class="hljs-constant" style="box-sizing: border-box;">Address</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0</span> is <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> stack<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d, malloc'</span>d <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">or</span> (recently) free<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">Process</span> terminating with default action of signal <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span> (<span class="hljs-constant" style="box-sizing: border-box;">SIGSEGV</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==  <span class="hljs-constant" style="box-sizing: border-box;">Access</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> within mapped region at address <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4006D5</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test1.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==  <span class="hljs-constant" style="box-sizing: border-box;">If</span> you believe this happened as a result of a stack
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==  overflow <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> your program<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'s main thread (unlikely but
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==  possible), you can try to increase the size of the
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==  main thread stack using the --main-stacksize= flag.
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==  <span class="hljs-constant" style="box-sizing: border-box;">The</span> main thread stack size used <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> this run was <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8388608</span>.
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">HEAP</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> use at <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">exit:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>==   total heap <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">usage:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> allocs, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> frees, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes allocated
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">All</span> heap blocks were freed -- no leaks are possible
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">For</span> counts of detected <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> suppressed errors, rerun <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">with:</span> -v
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">Use</span> --track-origins=yes to see where uninitialised values come from
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3978</span>== <span class="hljs-constant" style="box-sizing: border-box;">ERROR</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> errors from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> contexts (<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">suppressed:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li></ul>

1、3978是进程ID,最前面那个行是Valgrind的版本信息。 
2、后面指出使用未初始化的内存,大小为8,错误发生的位置。 
3、最后HEAP SUMMARY说明在HEAP上内存使用情况。 
整个输出大概就是以上几个部分。

越界使用内存

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *p=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>];
    *(p+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>)=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">delete</span>[] p;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

输出:

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">Memcheck</span>, a memory error detector
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">Copyright</span> (<span class="hljs-constant" style="box-sizing: border-box;">C</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2002</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2013</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">GNU</span> <span class="hljs-constant" style="box-sizing: border-box;">GPL</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d, by Julian Seward et al.
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">Using</span> <span class="hljs-constant" style="box-sizing: border-box;">Valgrind</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.10</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>.<span class="hljs-constant" style="box-sizing: border-box;">SVN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">LibVEX</span>; rerun with -h <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> copyright info
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">Command</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> ./test2
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">Invalid</span> write of size <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x40077B</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test2.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>==  <span class="hljs-constant" style="box-sizing: border-box;">Address</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x5a1d068</span> is <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes after a block of size <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> alloc<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4C2B800</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> operator new[](unsigned long) (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>==    by <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x40076E</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test2.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">HEAP</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>==     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> use at <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">exit:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>==   total heap <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">usage:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> allocs, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> frees, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> bytes allocated
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">All</span> heap blocks were freed -- no leaks are possible
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">For</span> counts of detected <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> suppressed errors, rerun <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">with:</span> -v
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4297</span>== <span class="hljs-constant" style="box-sizing: border-box;">ERROR</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> errors from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> contexts (<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">suppressed:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

内存泄漏

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *p=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>];
    *(p+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

输出:

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">Memcheck</span>, a memory error detector
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">Copyright</span> (<span class="hljs-constant" style="box-sizing: border-box;">C</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2002</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2013</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">GNU</span> <span class="hljs-constant" style="box-sizing: border-box;">GPL</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d, by Julian Seward et al.
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">Using</span> <span class="hljs-constant" style="box-sizing: border-box;">Valgrind</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.10</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>.<span class="hljs-constant" style="box-sizing: border-box;">SVN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">LibVEX</span>; rerun with -h <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> copyright info
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">Command</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> ./test3
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">HEAP</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>==     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> use at <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">exit:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>==   total heap <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">usage:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> allocs, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> frees, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> bytes allocated
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">LEAK</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>==    definitely <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">lost:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>==    indirectly <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">lost:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>==      possibly <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">lost:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>==    still <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">reachable:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>==         <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">suppressed:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">Rerun</span> with --leak-check=full to see details of leaked memory
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">For</span> counts of detected <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> suppressed errors, rerun <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">with:</span> -v
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4318</span>== <span class="hljs-constant" style="box-sizing: border-box;">ERROR</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> errors from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> contexts (<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">suppressed:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

两次释放内存

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *p=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>];
    *(p+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">delete</span>[] p;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">delete</span>[] p;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

输出:

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">Memcheck</span>, a memory error detector
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">Copyright</span> (<span class="hljs-constant" style="box-sizing: border-box;">C</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2002</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2013</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">GNU</span> <span class="hljs-constant" style="box-sizing: border-box;">GPL</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d, by Julian Seward et al.
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">Using</span> <span class="hljs-constant" style="box-sizing: border-box;">Valgrind</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.10</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>.<span class="hljs-constant" style="box-sizing: border-box;">SVN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">LibVEX</span>; rerun with -h <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> copyright info
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">Command</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> ./test4
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">Invalid</span> free() / delete / delete[] / realloc()
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4C2C83C</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> operator delete[](void*) (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>==    by <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4007A6</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test4.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>==  <span class="hljs-constant" style="box-sizing: border-box;">Address</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x5a1d040</span> is <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes inside a block of size <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> free<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4C2C83C</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> operator delete[](void*) (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>==    by <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x400793</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test4.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">HEAP</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>==     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> use at <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">exit:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>==   total heap <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">usage:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> allocs, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> frees, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> bytes allocated
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">All</span> heap blocks were freed -- no leaks are possible
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">For</span> counts of detected <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> suppressed errors, rerun <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">with:</span> -v
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4330</span>== <span class="hljs-constant" style="box-sizing: border-box;">ERROR</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> errors from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> contexts (<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">suppressed:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul>

使用已经释放的内存

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *p=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>];
    *(p+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">delete</span>[] p;
    *(p+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

输出:

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">Memcheck</span>, a memory error detector
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">Copyright</span> (<span class="hljs-constant" style="box-sizing: border-box;">C</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2002</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2013</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">GNU</span> <span class="hljs-constant" style="box-sizing: border-box;">GPL</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d, by Julian Seward et al.
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">Using</span> <span class="hljs-constant" style="box-sizing: border-box;">Valgrind</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.10</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>.<span class="hljs-constant" style="box-sizing: border-box;">SVN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">LibVEX</span>; rerun with -h <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> copyright info
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">Command</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> ./test5
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">Invalid</span> write of size <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x40079C</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test5.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>==  <span class="hljs-constant" style="box-sizing: border-box;">Address</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x5a1d048</span> is <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> bytes inside a block of size <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> free<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4C2C83C</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> operator delete[](void*) (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>==    by <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x400793</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test5.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">HEAP</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>==     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> use at <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">exit:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>==   total heap <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">usage:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> allocs, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> frees, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> bytes allocated
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">All</span> heap blocks were freed -- no leaks are possible
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">For</span> counts of detected <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> suppressed errors, rerun <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">with:</span> -v
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4341</span>== <span class="hljs-constant" style="box-sizing: border-box;">ERROR</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> errors from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> contexts (<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">suppressed:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

不匹配使用new/delete或new[]/delete[]

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> *p=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>];
    *(p+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">delete</span> p;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

输出:

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">Memcheck</span>, a memory error detector
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">Copyright</span> (<span class="hljs-constant" style="box-sizing: border-box;">C</span>) <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2002</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2013</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">GNU</span> <span class="hljs-constant" style="box-sizing: border-box;">GPL</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d, by Julian Seward et al.
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">Using</span> <span class="hljs-constant" style="box-sizing: border-box;">Valgrind</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.10</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>.<span class="hljs-constant" style="box-sizing: border-box;">SVN</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-constant" style="box-sizing: border-box;">LibVEX</span>; rerun with -h <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> copyright info
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">Command</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> ./test6
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">Mismatched</span> free() / delete / delete []
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4C2C2BC</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> operator delete(void*) (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>==    by <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x40078C</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test6.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>==  <span class="hljs-constant" style="box-sizing: border-box;">Address</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x5a1d040</span> is <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes inside a block of size <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> alloc<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'d
</span></span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>==    at <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4C2B800</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> operator new[](unsigned long) (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>==    by <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x40076E</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> main (test6.<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">cpp:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>)
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">HEAP</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span>
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>==     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> use at <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">exit:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> bytes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> blocks
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>==   total heap <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">usage:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> allocs, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> frees, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span> bytes allocated
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">All</span> heap blocks were freed -- no leaks are possible
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== 
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">For</span> counts of detected <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> suppressed errors, rerun <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">with:</span> -v
</span>=<span class="ruby" style="box-sizing: border-box;">=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4375</span>== <span class="hljs-constant" style="box-sizing: border-box;">ERROR</span> <span class="hljs-constant" style="box-sizing: border-box;">SUMMARY</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> errors from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> contexts (<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">suppressed:</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> from <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul>

你可能感兴趣的:(linux 使用valgrind检查内存使用问题)