I am running valgrind --leak-check=full test.cpp
on the following code
#include <iostream>
int* p = new int[42]; // no leak reported
int main()
{
p[0] = 42; // use it
std::cout << p[0];
}
and there is no leak reported:
==37293== LEAK SUMMARY:
==37293== definitely lost: 0 bytes in 0 blocks
==37293== indirectly lost: 0 bytes in 0 blocks
==37293== possibly lost: 0 bytes in 0 blocks
Whenever I move the definition int* p = new int[42];
inside main()
, so it has automatic storage duration, valgrind detects the memory leak. Why doesn't it detect the leak for static storage duration objects? Am I missing something here?
They're still reachable and so are not considered leaked. If you want to show even reachable blocks, pass --leak-check=full --show-leak-kinds=all
to valgrind.
Generally, this kind of "leak" is not a bug. In your example code, there is no "right place" to put a corresponding delete
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With