I was using valgrind to find memory usage by my program using this command
valgrind --tool=memcheck --leak-check=full -s --track-origins=yes ./memoryProblem
it shows that the total heap usage by my program was 72,704 bytes
this is my program
#include <iostream>
int main(int argc, char const *argv[])
{
int a[32768];
std::cout << sizeof a;
return 0;
}
an int is 4 bytes and 32768*4 should be 131,072 bytes which is also the output of the program but then why is valgrind showing heap usage for an array on stack?
Moreover, I removed iostream and cout and reduced size of array to 10 integers and this was the output:
==169343== Memcheck, a memory error detector
==169343== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==169343== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==169343== Command: ./memoryProblem
==169343==
==169343==
==169343== HEAP SUMMARY:
==169343== in use at exit: 0 bytes in 0 blocks
==169343== total heap usage: 1 allocs, 1 frees, 72,704 bytes allocated
==169343==
==169343== All heap blocks were freed -- no leaks are possible
==169343==
==169343== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
for the program:
int main(int argc, char const *argv[])
{
int a[10];
return 0;
}
why is it showing 72,704 bytes?
and i did not forget to compile my program
Do C++ programs use all that memory, I'm interested in how they work or is it valgrind using that memory
i tried changing valgrind command to
valgrind --tool=memcheck ./memoryProblem
but same result