Valgrind does not detect leak in global pointer

I run valgrind --leak-check=full test.cpp in 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 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 an automatic storage duration, valgrind detects a memory leak. Why doesn't it detect a leak of static storage objects? Did I miss something?

+6
source share
1 answer

They are still available and therefore are not considered leaked. If you want to show even the available blocks, go --leak-check=full --show-leak-kinds=all to valgrind.

As a rule, such a “leak” is not a mistake. In your sample code, there is no “right place” to place the corresponding delete .

+6
source

Source: https://habr.com/ru/post/988063/


All Articles