1

Installed valgrind on my WSL and I was a bit confused.

Problem: I get a lot of allocated bytes even though my program doesn't do anything.

main.cpp:

#include <iostream>

using namespace std;

int main() {

     cout << "I'm testing valgrind!" << endl;
     return 0;
}

The result of calling valgrind:

==435== Memcheck, a memory error detector
==435== Copyright (C) 2002-2017, and GNU GPLd, by Julian Seward et al.
==435== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==435== Command: ./start
==435== 
==435== error calling PR_SET_PTRACER, vgdb might block 
I'm testing valgrind!
==435== 
==435== HEAP SUMMARY:
==435==     in use at exit: 0 bytes in 0 blocks
==435==   total heap usage: 2 allocs, 2 frees, 73,728 bytes allocated
==435== 
==435== All heap blocks were freed -- no leaks are possible
==435== 
==435== For lists of detected and suppressed errors, rerun with: -s
==435== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

I actually have two questions:

  1. Why do I get so many allocated bytes?
  2. How do I get rid of this? (In my univercity server valgrind doesn't show these bytes so I was wondering if I could do that too..)
  • 1
    Your implementation of `std::cout` is allocating (and then freeing) those bytes. It's not something to worry about. – Drew Dormann Oct 10 '22 at 19:34
  • How's that _"many"_?? Ofc `std::cin` requires some bytes to be allocated. – πάντα ῥεῖ Oct 10 '22 at 19:34
  • 1
    *"All heap blocks were freed -- no leaks are possible"* - that's the conclusion you care about, first and foremost. *Then* you start caring about rampantly large memory consumption (of which this has none). – WhozCraig Oct 10 '22 at 20:02
  • `73,728` is `0x12000` in hex so presumably something allocated a 64k buffer and something else allocated an 8k one. My guess would be one for `std::cout` stream and one for the `stdout` file – Alan Birtles Oct 10 '22 at 20:14

1 Answers1

1

If you are curious and want to see what these allocations are, run with

--run-libc-freeres=no --run-cxx-freeres=no -s --leak-check=full --show-reachable=yes

As said in the comments, don't worry about this. All that matters are the errors.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43