C语言 理解valgrind输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2479684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:53:39  来源:igfitidea点击:

understanding valgrind output

cmemory-leaksvalgrind

提问by sbsp

i made a post earlier asking about checking for memory leaks etc, i did say i wasnt to familiar with the terminal in linux but someone said to me it was easy with valgrind

我早些时候发了一篇关于检查内存泄漏等问题的帖子,我确实说过我不熟悉 linux 中的终端,但有人对我说 valgrind 很容易

i have managed to get it running etc but not to sure what the output means. Glancing over, all looks good to me but would like to run it past you experience folk for confirmation if possible. THe output is as follows

我已经设法让它运行等,但不确定输出的含义。看一眼,我觉得一切都很好,但如果可能的话,我想通过你的经验来确认。输出如下

^C==2420== 
==2420== HEAP SUMMARY:
==2420==     in use at exit: 2,240 bytes in 81 blocks
==2420==   total heap usage: 82 allocs, 1 frees, 2,592 bytes allocated
==2420== 
==2420== LEAK SUMMARY:
==2420==    definitely lost: 0 bytes in 0 blocks
==2420==    indirectly lost: 0 bytes in 0 blocks
==2420==      possibly lost: 0 bytes in 0 blocks
==2420==    still reachable: 2,240 bytes in 81 blocks
==2420==         suppressed: 0 bytes in 0 blocks
==2420== Reachable blocks (those to which a pointer was found) are not shown.
==2420== To see them, rerun with: --leak-check=full --show-reachable=yes
==2420== 
==2420== For counts of detected and suppressed errors, rerun with: -v
==2420== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 8)

Is all good here? the only thing concerning me is the still reachable part. Is that ok?

这里一切都好吗?唯一与我有关的是仍然可以到达的部分。这可以吗?

Thanks everyone

谢谢大家

回答by mctylr

I suggest you stop, and read the Valgrind Quick Start, paying particular attention to section 4, "Interpreting Memcheck's output," and look over the FAQ.

我建议你停下来,阅读 Valgrind快速入门,特别注意第 4 节“解释 Memcheck 的输出”,并查看常见问题解答



Afterward, I think you could benefit from reading How to Ask Questions The Smart Way(aka Smart Questions) to improve your problem solving skills, and improve your asking for assistance in community forums like StackOverflow, where better questions are rewarded with better answers.

之后,我认为您可以从阅读How to Ask Questions The Smart Way(又名 Smart Questions)中受益,以提高您解决问题的能力,并改善您在StackOverflow等社区论坛中寻求帮助的能力,更好的问题会得到更好的答案。

This is not intended to be an insult or personal attack, but a suggestion on how you can ask questions better, so you can get better answers. You will also learn how to answer your own basic questions yourself more often in the process, speeding up your overall efforts. Good luck.

这不是侮辱或人身攻击,而是关于如何更好地提出问题的建议,以便您可以获得更好的答案。您还将学习如何在此过程中更频繁地自己回答自己的基本问题,从而加快您的整体努力。祝你好运。

回答by laginimaineb

The output you pasted shows:

您粘贴的输出显示:

==2420== total heap usage: 82 allocs, 1 frees, 2,592 bytes allocated

...

==2420== still reachable: 2,240 bytes in 81 blocks

==2420== 总堆使用量:82 次分配,1 次释放,已分配 2,592 字节

...

==2420== 仍然可达:81 个块中有 2,240 个字节

82 allocations and only one free, so in the end there are 81 blocks still 'reachable' on the heap. As the Valgrind FAQstates, this may indicate that the code uses some memory pool allocator and therefore does not free memory as soon as it's unused, but rather keeps it for later use, or it may actually be a memory leak (unlikely, though). Follow the steps in the link to check whether this is due to the STLs use of memory caching.

82 次分配,只有 1 次空闲,因此最终堆上仍有 81 个“可访问”块。正如Valgrind FAQ所述,这可能表明代码使用了一些内存池分配器,因此不会在内存未使用时立即释放内存,而是将其保留以备后用,或者它实际上可能是内存泄漏(虽然不太可能) . 按照链接中的步骤检查这是否是由于 STL 使用内存缓存造成的。