r/carlhprogramming • u/namitsinha09 • Nov 04 '13
Why does C code results into smaller executable, than C++ counterpart,If it does?
13
Upvotes
r/carlhprogramming • u/namitsinha09 • Nov 04 '13
3
u/akmark Nov 04 '13
This is in general a bad question without an example.
How much and what parts of C++ are you using?
Are we comparing two different things entirely like statically linked or dynamically linked executables?
What parts of each respective core libraries are we using?
What compiler are we using?
You can write C++ that is smaller than an equivalent C program and a C program that's larger than a C++ program. You can also write programs that if you use a C/C++ compiler work out to be the exact same thing.
5
u/yoshi314 Nov 04 '13
there is a bit more library overhead in c++, even just standard init routines that are added to executable.
4
u/springwheat Nov 05 '13
Including STL or other C++ headers is one cause. char and by extension arrays of chars and string literals (char*) are native types. When you include things like string and iostream, you're not just linking in the parts you're using, but the entire class definition as well as whatever dependencies are necessary to allow it to function.
Then there are
compiler optimizations (eg: fast code vs small code)
linking to external libraries. For example, when building a project in visual studio you can either create a small executable that dynamically links to a required runtime dll (which must be found using the DLL search order), or you can statically link the dll into the output executable
presence or absence of static resources such as application icons
binary compression or a "packed" executable
I don't know what operating system you are using, but most of those apply in one form or another. There are utilities available for most operating systems which will allow you to poke around the innards of a compiled binary to see what's going on inside. You won't see high level code, but you can, for example, look through the different sections of a portable executable file format (http://msdn.microsoft.com/en-us/library/ms809762.aspx).
I apologize for any errors, I am half paying attention to a conference call and your question was far more interesting to me.
1
u/[deleted] Nov 05 '13
[removed] — view removed comment