N/APosted on - 06/26/2019
If you use the same code, simply change the compiler (from a C compiler to a C ++ compiler) to change the amount of memory allocated. I do not know why and I would like to understand better. So far, the best answer I’ve received is “probably the I / O streams”, which is not very descriptive and puzzles me on the aspect “you do not pay for what you do not use”. Can you tell me where I am wrong?
Size Of C++ Allocation Is More Than C
The heap usage comes from the standard C ++ library. It reserves memory for the use of the internal library at startup. If you do not link, there should be no difference between C and C ++ versions. With GCC and Clang, you can compile the file with:
g ++ -Wl, – if needed main.cpp
This tells the linker not to create links to unused libraries. Your code sample does not use the C ++ library, so it should not be bound to the standard C ++ library.
You can also test this with file C. If you compile with:
gcc main.c -lstdc ++
The heap usage reappears even if you have created a C program.