Home >Backend Development >C++ >Why Does Valgrind Report Memory Leaks in a Trivial C Program Using ``?

Why Does Valgrind Report Memory Leaks in a Trivial C Program Using ``?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-15 17:00:17277browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn