Home >Backend Development >C++ >Why Does Valgrind Report Memory Leaks in a Trivial C Program Using ``?
Valgrind: Memory still reachable with trivial program using
Question:
Why does valgrind report that there are still reachable bytes when running the following trivial program:
#include <iostream> int main() { return 0; }
Answer:
Valgrind reports that there are still reachable bytes due to the way C standard libraries manage memory. Many implementations use memory pool allocators that keep memory for destructed objects for later reuse. These pools are not freed when the program exits, which causes Valgrind to report the memory as still reachable.
To force the STL to use malloc and free memory immediately, you can set the environment variable GLIBCXX_FORCE_NEW before running your program. This will likely slow down your program, but will eliminate the Valgrind reports.
The above is the detailed content of Why Does Valgrind Report Memory Leaks in a Trivial C Program Using ``?. For more information, please follow other related articles on the PHP Chinese website!