Why is C++ initial allocation so much larger than C's?How much faster is C++ than C#?Why do we need virtual functions in C++?Why should C++ programmers minimize use of 'new'?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?C++ code for testing the Collatz conjecture faster than hand-written assembly - why?C vs C++ compilation incompatibility - does not name a typeWhy is 2 * (i * i) faster than 2 * i * i in Java?

Should my manager be aware of private LinkedIn approaches I receive? How to politely have this happen?

Wifi dongle speed is slower than advertised

Why do some professors with PhDs leave their professorships to teach high school?

Does squid ink pasta bleed?

How can I politely work my way around not liking coffee or beer when it comes to professional networking?

Can White Castle?

C-152 carb heat on before landing in hot weather?

What are the penalties for overstaying in USA?

Would it be a copyright violation if I made a character’s full name refer to a song?

Why doesn't a marching band have strings?

Folding basket - is there such a thing?

expiry or manufactured date?

Hand soldering SMD 1206 components

How much will studying magic in an academy cost?

Where can I find a database of galactic spectra?

If I wouldn't want to read the story, is writing it still a good idea?

Should developer taking test phones home or put in office?

Underbar nabla symbol doesn't work

Why do some games show lights shine thorugh walls?

In the Marvel universe, can a human have a baby with any non-human?

Sci fi short story, robot city that nags people about health

Vanishing of certain coefficients coming from Coxeter groups

Is my Rep in Stack-Exchange Form?

Does this Wild Magic result affect the sorcerer or just other creatures?



Why is C++ initial allocation so much larger than C's?


How much faster is C++ than C#?Why do we need virtual functions in C++?Why should C++ programmers minimize use of 'new'?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?C++ code for testing the Collatz conjecture faster than hand-written assembly - why?C vs C++ compilation incompatibility - does not name a typeWhy is 2 * (i * i) faster than 2 * i * i in Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








9















When using the same code, simply changing the compiler (from a C compiler to a C++ compiler) will change how much memory is allocated. I'm not quite sure why this is and would like to understand it more. So far the best response I've gotten is "probably the IO streams", which isn't very descriptive and makes me wonder about the "you don't pay for what you don't use" aspect of C++.



I'm using the clang and gcc compilers, versions 7.0.1-8 and 8.3.0-6 respectively. My system is running on Debian 10 (Buster), latest. The benchmarks are done via Valgrind Massif.



#include <stdio.h>

int main()
printf("Hello, world!n");
return 0;



The code used does not change, but whether I compile as C or as C++ changes the results of the Valgrind benchmark. The values remain consistent across compilers, however.
The runtime allocations (peak) for the program go as follows:



  • GCC (C): 1,032 bytes (1KB)

  • G++ (C++): 73,744 bytes, (~74kb)

  • Clang (C): 1,032 bytes (1KB)

  • Clang++ (C++): 73,744 bytes (~74kb)

For compiling, I use the following commands:



clang -O3 -o c-clang ./main.c
gcc -O3 -o c-gcc ./main.c


clang++ -O3 -o cpp-clang ./main.cpp
g++ -O3 -o cpp-gcc ./main.cpp


For Valgrind, I run valgrind --tool=massif --massif-out-file=m_compiler_lang ./compiler-lang on each compiler and language, then ms_print for displaying the peaks.



Am I doing something wrong here?










share|improve this question









New contributor



Rerumu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 4





    To begin with, how are you building? What options do you use? And how do you measure? How do you run Valgrind?

    – Some programmer dude
    9 hours ago











  • This is C, not C++. C that compiles with a C++ compiler is just malformed C++. Regardless, C++ has more memory overhead than C as you can already tell.

    – bigwillydos
    9 hours ago











  • Hypothesis: this allocation comes from the standard library. There's more in the C++ standard library. I don't know where 73kb extra comes from, but maybe loading a shared library counts as an allocation.

    – Justin
    9 hours ago






  • 5





    If I remember correctly, modern C++ compilers have to an exception model where there is no performance hit to entering a try block at the expense of a larger memory footprint, maybe with a jump table or something. Maybe try compiling without exceptions and see what impact that has. Edit : In fact, iteratively try disabling various c++ features to see what impact that has on the memory footprint.

    – François Andrieux
    9 hours ago







  • 1





    When compiling with clang++ -xc instead of clang, the same allocation was there, which strongly suggests its due to linked libraries

    – Justin
    9 hours ago

















9















When using the same code, simply changing the compiler (from a C compiler to a C++ compiler) will change how much memory is allocated. I'm not quite sure why this is and would like to understand it more. So far the best response I've gotten is "probably the IO streams", which isn't very descriptive and makes me wonder about the "you don't pay for what you don't use" aspect of C++.



I'm using the clang and gcc compilers, versions 7.0.1-8 and 8.3.0-6 respectively. My system is running on Debian 10 (Buster), latest. The benchmarks are done via Valgrind Massif.



#include <stdio.h>

int main()
printf("Hello, world!n");
return 0;



The code used does not change, but whether I compile as C or as C++ changes the results of the Valgrind benchmark. The values remain consistent across compilers, however.
The runtime allocations (peak) for the program go as follows:



  • GCC (C): 1,032 bytes (1KB)

  • G++ (C++): 73,744 bytes, (~74kb)

  • Clang (C): 1,032 bytes (1KB)

  • Clang++ (C++): 73,744 bytes (~74kb)

For compiling, I use the following commands:



clang -O3 -o c-clang ./main.c
gcc -O3 -o c-gcc ./main.c


clang++ -O3 -o cpp-clang ./main.cpp
g++ -O3 -o cpp-gcc ./main.cpp


For Valgrind, I run valgrind --tool=massif --massif-out-file=m_compiler_lang ./compiler-lang on each compiler and language, then ms_print for displaying the peaks.



Am I doing something wrong here?










share|improve this question









New contributor



Rerumu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 4





    To begin with, how are you building? What options do you use? And how do you measure? How do you run Valgrind?

    – Some programmer dude
    9 hours ago











  • This is C, not C++. C that compiles with a C++ compiler is just malformed C++. Regardless, C++ has more memory overhead than C as you can already tell.

    – bigwillydos
    9 hours ago











  • Hypothesis: this allocation comes from the standard library. There's more in the C++ standard library. I don't know where 73kb extra comes from, but maybe loading a shared library counts as an allocation.

    – Justin
    9 hours ago






  • 5





    If I remember correctly, modern C++ compilers have to an exception model where there is no performance hit to entering a try block at the expense of a larger memory footprint, maybe with a jump table or something. Maybe try compiling without exceptions and see what impact that has. Edit : In fact, iteratively try disabling various c++ features to see what impact that has on the memory footprint.

    – François Andrieux
    9 hours ago







  • 1





    When compiling with clang++ -xc instead of clang, the same allocation was there, which strongly suggests its due to linked libraries

    – Justin
    9 hours ago













9












9








9








When using the same code, simply changing the compiler (from a C compiler to a C++ compiler) will change how much memory is allocated. I'm not quite sure why this is and would like to understand it more. So far the best response I've gotten is "probably the IO streams", which isn't very descriptive and makes me wonder about the "you don't pay for what you don't use" aspect of C++.



I'm using the clang and gcc compilers, versions 7.0.1-8 and 8.3.0-6 respectively. My system is running on Debian 10 (Buster), latest. The benchmarks are done via Valgrind Massif.



#include <stdio.h>

int main()
printf("Hello, world!n");
return 0;



The code used does not change, but whether I compile as C or as C++ changes the results of the Valgrind benchmark. The values remain consistent across compilers, however.
The runtime allocations (peak) for the program go as follows:



  • GCC (C): 1,032 bytes (1KB)

  • G++ (C++): 73,744 bytes, (~74kb)

  • Clang (C): 1,032 bytes (1KB)

  • Clang++ (C++): 73,744 bytes (~74kb)

For compiling, I use the following commands:



clang -O3 -o c-clang ./main.c
gcc -O3 -o c-gcc ./main.c


clang++ -O3 -o cpp-clang ./main.cpp
g++ -O3 -o cpp-gcc ./main.cpp


For Valgrind, I run valgrind --tool=massif --massif-out-file=m_compiler_lang ./compiler-lang on each compiler and language, then ms_print for displaying the peaks.



Am I doing something wrong here?










share|improve this question









New contributor



Rerumu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











When using the same code, simply changing the compiler (from a C compiler to a C++ compiler) will change how much memory is allocated. I'm not quite sure why this is and would like to understand it more. So far the best response I've gotten is "probably the IO streams", which isn't very descriptive and makes me wonder about the "you don't pay for what you don't use" aspect of C++.



I'm using the clang and gcc compilers, versions 7.0.1-8 and 8.3.0-6 respectively. My system is running on Debian 10 (Buster), latest. The benchmarks are done via Valgrind Massif.



#include <stdio.h>

int main()
printf("Hello, world!n");
return 0;



The code used does not change, but whether I compile as C or as C++ changes the results of the Valgrind benchmark. The values remain consistent across compilers, however.
The runtime allocations (peak) for the program go as follows:



  • GCC (C): 1,032 bytes (1KB)

  • G++ (C++): 73,744 bytes, (~74kb)

  • Clang (C): 1,032 bytes (1KB)

  • Clang++ (C++): 73,744 bytes (~74kb)

For compiling, I use the following commands:



clang -O3 -o c-clang ./main.c
gcc -O3 -o c-gcc ./main.c


clang++ -O3 -o cpp-clang ./main.cpp
g++ -O3 -o cpp-gcc ./main.cpp


For Valgrind, I run valgrind --tool=massif --massif-out-file=m_compiler_lang ./compiler-lang on each compiler and language, then ms_print for displaying the peaks.



Am I doing something wrong here?







c++ c benchmarking






share|improve this question









New contributor



Rerumu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Rerumu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 9 hours ago







Rerumu













New contributor



Rerumu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 9 hours ago









RerumuRerumu

485 bronze badges




485 bronze badges




New contributor



Rerumu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Rerumu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









  • 4





    To begin with, how are you building? What options do you use? And how do you measure? How do you run Valgrind?

    – Some programmer dude
    9 hours ago











  • This is C, not C++. C that compiles with a C++ compiler is just malformed C++. Regardless, C++ has more memory overhead than C as you can already tell.

    – bigwillydos
    9 hours ago











  • Hypothesis: this allocation comes from the standard library. There's more in the C++ standard library. I don't know where 73kb extra comes from, but maybe loading a shared library counts as an allocation.

    – Justin
    9 hours ago






  • 5





    If I remember correctly, modern C++ compilers have to an exception model where there is no performance hit to entering a try block at the expense of a larger memory footprint, maybe with a jump table or something. Maybe try compiling without exceptions and see what impact that has. Edit : In fact, iteratively try disabling various c++ features to see what impact that has on the memory footprint.

    – François Andrieux
    9 hours ago







  • 1





    When compiling with clang++ -xc instead of clang, the same allocation was there, which strongly suggests its due to linked libraries

    – Justin
    9 hours ago












  • 4





    To begin with, how are you building? What options do you use? And how do you measure? How do you run Valgrind?

    – Some programmer dude
    9 hours ago











  • This is C, not C++. C that compiles with a C++ compiler is just malformed C++. Regardless, C++ has more memory overhead than C as you can already tell.

    – bigwillydos
    9 hours ago











  • Hypothesis: this allocation comes from the standard library. There's more in the C++ standard library. I don't know where 73kb extra comes from, but maybe loading a shared library counts as an allocation.

    – Justin
    9 hours ago






  • 5





    If I remember correctly, modern C++ compilers have to an exception model where there is no performance hit to entering a try block at the expense of a larger memory footprint, maybe with a jump table or something. Maybe try compiling without exceptions and see what impact that has. Edit : In fact, iteratively try disabling various c++ features to see what impact that has on the memory footprint.

    – François Andrieux
    9 hours ago







  • 1





    When compiling with clang++ -xc instead of clang, the same allocation was there, which strongly suggests its due to linked libraries

    – Justin
    9 hours ago







4




4





To begin with, how are you building? What options do you use? And how do you measure? How do you run Valgrind?

– Some programmer dude
9 hours ago





To begin with, how are you building? What options do you use? And how do you measure? How do you run Valgrind?

– Some programmer dude
9 hours ago













This is C, not C++. C that compiles with a C++ compiler is just malformed C++. Regardless, C++ has more memory overhead than C as you can already tell.

– bigwillydos
9 hours ago





This is C, not C++. C that compiles with a C++ compiler is just malformed C++. Regardless, C++ has more memory overhead than C as you can already tell.

– bigwillydos
9 hours ago













Hypothesis: this allocation comes from the standard library. There's more in the C++ standard library. I don't know where 73kb extra comes from, but maybe loading a shared library counts as an allocation.

– Justin
9 hours ago





Hypothesis: this allocation comes from the standard library. There's more in the C++ standard library. I don't know where 73kb extra comes from, but maybe loading a shared library counts as an allocation.

– Justin
9 hours ago




5




5





If I remember correctly, modern C++ compilers have to an exception model where there is no performance hit to entering a try block at the expense of a larger memory footprint, maybe with a jump table or something. Maybe try compiling without exceptions and see what impact that has. Edit : In fact, iteratively try disabling various c++ features to see what impact that has on the memory footprint.

– François Andrieux
9 hours ago






If I remember correctly, modern C++ compilers have to an exception model where there is no performance hit to entering a try block at the expense of a larger memory footprint, maybe with a jump table or something. Maybe try compiling without exceptions and see what impact that has. Edit : In fact, iteratively try disabling various c++ features to see what impact that has on the memory footprint.

– François Andrieux
9 hours ago





1




1





When compiling with clang++ -xc instead of clang, the same allocation was there, which strongly suggests its due to linked libraries

– Justin
9 hours ago





When compiling with clang++ -xc instead of clang, the same allocation was there, which strongly suggests its due to linked libraries

– Justin
9 hours ago












1 Answer
1






active

oldest

votes


















11














The heap usage comes from the C++ standard library. It allocates memory for internal library use on startup. If you don't link against it, there should be zero difference between the C and C++ version. With GCC and Clang, you can compile the file with:




g++ -Wl,--as-needed main.cpp


This will instruct the linker to not link against unused libraries. In your example code, the C++ library is not used, so it should not link against the C++ standard library.



You can also test this with the C file. If you compile with:




gcc main.c -lstdc++


The heap usage will reappear, even though you've built a C program.






share|improve this answer

























  • On seeing this answer, my first thought is, "If this flag helps reduce unneeded overhead, why isn't it on by default?". Is there a good answer to that?

    – Nat
    13 mins ago













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Rerumu is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56692117%2fwhy-is-c-initial-allocation-so-much-larger-than-cs%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









11














The heap usage comes from the C++ standard library. It allocates memory for internal library use on startup. If you don't link against it, there should be zero difference between the C and C++ version. With GCC and Clang, you can compile the file with:




g++ -Wl,--as-needed main.cpp


This will instruct the linker to not link against unused libraries. In your example code, the C++ library is not used, so it should not link against the C++ standard library.



You can also test this with the C file. If you compile with:




gcc main.c -lstdc++


The heap usage will reappear, even though you've built a C program.






share|improve this answer

























  • On seeing this answer, my first thought is, "If this flag helps reduce unneeded overhead, why isn't it on by default?". Is there a good answer to that?

    – Nat
    13 mins ago















11














The heap usage comes from the C++ standard library. It allocates memory for internal library use on startup. If you don't link against it, there should be zero difference between the C and C++ version. With GCC and Clang, you can compile the file with:




g++ -Wl,--as-needed main.cpp


This will instruct the linker to not link against unused libraries. In your example code, the C++ library is not used, so it should not link against the C++ standard library.



You can also test this with the C file. If you compile with:




gcc main.c -lstdc++


The heap usage will reappear, even though you've built a C program.






share|improve this answer

























  • On seeing this answer, my first thought is, "If this flag helps reduce unneeded overhead, why isn't it on by default?". Is there a good answer to that?

    – Nat
    13 mins ago













11












11








11







The heap usage comes from the C++ standard library. It allocates memory for internal library use on startup. If you don't link against it, there should be zero difference between the C and C++ version. With GCC and Clang, you can compile the file with:




g++ -Wl,--as-needed main.cpp


This will instruct the linker to not link against unused libraries. In your example code, the C++ library is not used, so it should not link against the C++ standard library.



You can also test this with the C file. If you compile with:




gcc main.c -lstdc++


The heap usage will reappear, even though you've built a C program.






share|improve this answer















The heap usage comes from the C++ standard library. It allocates memory for internal library use on startup. If you don't link against it, there should be zero difference between the C and C++ version. With GCC and Clang, you can compile the file with:




g++ -Wl,--as-needed main.cpp


This will instruct the linker to not link against unused libraries. In your example code, the C++ library is not used, so it should not link against the C++ standard library.



You can also test this with the C file. If you compile with:




gcc main.c -lstdc++


The heap usage will reappear, even though you've built a C program.







share|improve this answer














share|improve this answer



share|improve this answer








edited 8 hours ago

























answered 8 hours ago









Nikos C.Nikos C.

38.6k5 gold badges43 silver badges74 bronze badges




38.6k5 gold badges43 silver badges74 bronze badges












  • On seeing this answer, my first thought is, "If this flag helps reduce unneeded overhead, why isn't it on by default?". Is there a good answer to that?

    – Nat
    13 mins ago

















  • On seeing this answer, my first thought is, "If this flag helps reduce unneeded overhead, why isn't it on by default?". Is there a good answer to that?

    – Nat
    13 mins ago
















On seeing this answer, my first thought is, "If this flag helps reduce unneeded overhead, why isn't it on by default?". Is there a good answer to that?

– Nat
13 mins ago





On seeing this answer, my first thought is, "If this flag helps reduce unneeded overhead, why isn't it on by default?". Is there a good answer to that?

– Nat
13 mins ago












Rerumu is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Rerumu is a new contributor. Be nice, and check out our Code of Conduct.












Rerumu is a new contributor. Be nice, and check out our Code of Conduct.











Rerumu is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56692117%2fwhy-is-c-initial-allocation-so-much-larger-than-cs%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу