std :: cout导致内存泄漏

时间:2015-05-26 20:20:33

标签: c++ memory-leaks valgrind

我有一个非常简单的C ++程序。

#include <iostream>

int main()
{
    std::cout << "HI" << std::endl;
    return 0;
}

我使用命令c++ --std=c++11 leak.cpp在Mac上编译它。

当我使用valgrind --leak-check=full ./a.out调试时,我得到以下输出:

==2187== HEAP SUMMARY:
==2187==     in use at exit: 38,906 bytes in 429 blocks
==2187==   total heap usage: 508 allocs, 79 frees, 45,074 bytes allocated
==2187== 
==2187== LEAK SUMMARY:
==2187==    definitely lost: 0 bytes in 0 blocks
==2187==    indirectly lost: 0 bytes in 0 blocks
==2187==      possibly lost: 0 bytes in 0 blocks
==2187==    still reachable: 4,096 bytes in 1 blocks
==2187==         suppressed: 34,810 bytes in 428 blocks
==2187== Reachable blocks (those to which a pointer was found) are not shown.
==2187== To see them, rerun with: --leak-check=full --show-leak-kinds=all

原来有4096个字节是&#34;仍然可以到达&#34;。如果我删除了cout语句,那么就不会再有&#34;仍然可以访问&#34;字节。

为什么输出到std::cout会导致内存泄漏?

1 个答案:

答案 0 :(得分:3)

在泄漏报告中可能是误报。 Valgrind只能如此聪明;你的标准库实现正在取得Valgrind没有特殊情况的某些自由。

我更担心弄清楚为什么这个小程序执行508次分配,总计45,074字节。