Std :: cout causes memory leak

I have a very simple C ++ program.

#include <iostream>

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

      

I will compile this on Mac using the command c++ --std=c++11 leak.cpp

.

When I debug this with valgrind --leak-check=full ./a.out

I get the following output:

==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

      

It turns out there are 4096 bytes that are "still available". If I remove the instruction cout

, then there are no more "still reachable" bytes.

Why is it that the output is not std::cout

causing a memory leak?

+3


source to share


1 answer


This could be a false positive in a leak report. Valgrind can be so smart; your standard library implementation accepts certain freedoms that Valgrind has no special case for.



I'm more worried about figuring out why this tiny program is doing 508 allocations, for a total of 45,074 bytes.

+3


source







All Articles