【发布时间】:2021-04-17 16:52:24
【问题描述】:
考虑下面的代码
int main(){
return 0;
}
我用 g++ 编译它并将输出传递给 valgrind。输出如下。
==11752== HEAP SUMMARY:
==11752== in use at exit: 72,704 bytes in 1 blocks
==11752== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==11752==
==11965== LEAK SUMMARY:
==11965== definitely lost: 0 bytes in 0 blocks
==11965== indirectly lost: 0 bytes in 0 blocks
==11965== possibly lost: 0 bytes in 0 blocks
==11965== still reachable: 72,704 bytes in 1 blocks
==11965== suppressed: 0 bytes in 0 blocks
但是,使用 gcc 在 C 中编译相同的代码会产生以下 valgrind 输出:
==11771== HEAP SUMMARY:
==11771== in use at exit: 0 bytes in 0 blocks
==11771== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==11771==
==11771== All heap blocks were freed -- no leaks are possible
好像在编译
看起来空的 C++ 程序实际上分配了内存并且没有释放它(这不是一场灾难,因为它是一个“仍然可以到达”的泄漏),我不知道为什么会发生这种情况。
我用 g++ 6.3 在 linux (solus os) 上做了这个测试。
有人能解释一下发生了什么吗?
【问题讨论】:
-
无法在 gcc 4.7.1 上重现。可能是 6.3 版本的 bug,请考虑报告。