0

I recently learned about the memory error checking tool, Valgrind. I checked for allocs on some very simple C++ code and found some interesting results which is very weird. For eg.,

#include<iostream>
using namespace std;

int main(){
        return 0;
}

And the valgrind ouput is:

$ valgrind ./a.out 
==16952== Memcheck, a memory error detector
==16952== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==16952== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==16952== Command: ./a.out
==16952== 
==16952== 
==16952== HEAP SUMMARY:
==16952==     in use at exit: 72,704 bytes in 1 blocks
==16952==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==16952== 
==16952== LEAK SUMMARY:
==16952==    definitely lost: 0 bytes in 0 blocks
==16952==    indirectly lost: 0 bytes in 0 blocks
==16952==      possibly lost: 0 bytes in 0 blocks
==16952==    still reachable: 72,704 bytes in 1 blocks
==16952==         suppressed: 0 bytes in 0 blocks
==16952== Rerun with --leak-check=full to see details of leaked memory
==16952== 
==16952== For counts of detected and suppressed errors, rerun with: -v
==16952== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Anybody please explain the mysterious alloc on this line:

==16952==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
  • 2
    There is always some memory allocated before `main`, and since you are using iostreams, I'm fairly certain that it allocates a bit of memory too. When using valgrind, the point isn't to be concerned about allocated memory, but about allocated memory that isn't freed (definitely lost, indirectly lost, possibly lost) – Justin Mar 09 '18 at 18:26
  • 4
    Who knows what `` is allocating. – François Andrieux Mar 09 '18 at 18:26
  • 1
    Most importing thing to learn using test tools: What is a "false positiv" :-) Maybe things like cin/cout will be not deconstructed and maybe some other global objects are alive until the process is terminated. Important to know about that you do not search in your real world code :-) – Klaus Mar 09 '18 at 18:29
  • Ok... then I will consider this as a silly error made by valgrind. :-) – Kaushal Kishore Mar 09 '18 at 18:31
  • 2
    @KaushalKishore This is not an error by valgrind. This is the expected behavior. It's reporting that there were allocations, but it explicitly says that there were no leaks. It always gives a report, whether or not it reports an error. This isn't a false positive – Justin Mar 09 '18 at 18:38
  • Thanks @Justin for clarifying this topic. – Kaushal Kishore Mar 09 '18 at 19:17

0 Answers0