Some more words on this. I am using Cygwin GCC 3.4.4.
Code: Select all
#include <stdio.h>
int main() {
printf( "I am alive! Beware.\n" );
getchar();
return 0;
}
Unstripped 9167 bytes, stripped 3072 bytes.
Code: Select all
#include <cstdio>
int main() {
std::printf( "I am alive! Beware.\n" );
std::getchar();
return 0;
}
Unstripped 9014 bytes, stripped 3072 bytes.
Finding #1: Just running GCC in C++ mode doesn't add to the binary size.
Code: Select all
#include <cstdio>
#include <string>
int main() {
std::printf( "I am alive! Beware.\n" );
std::getchar();
return 0;
}
Unstripped 9014 bytes, stripped 3072 bytes.
Finding #2: Adding a C++ header does not add to the binary size.
Code: Select all
#include <iostream>
#include <cstdio>
int main() {
std::printf( "I am alive! Beware.\n" );
std::getchar();
return 0;
}
Unstripped 477999 bytes, stripped 276992 bytes.
Whoa, what happened there?
The answer is: Global objects (as Candy said).
The C subset example didn't need to link anything C++-specific. But <iostream> gives you cin and cout (and cerr and clog and the wide-character counterparts), which have to be initialized no matter whether you use them or not. This in turn requires the supporting C++ runtime: Global constructor calling and exception support, mostly.
"Yuck, why should I use C++ then?"
The answer is threefold.
One, most of this is a
one-time cost. We are not looking at a size factor of 100 here, but a
fixed cost of 200k supporting code. For any non-trivial application, the cost of exception support in code size is neglectable compared to the application proper. With
good C++ code, you will actually reach a break-even point pretty soon where the C++ exception support is
smaller than the code required in C to check each and every return code.
Two, if you don't want to use the library
or exceptions, you can simply disable them altogether. It's all about choice.
Three,
abstraction. I'll let code speak for me here.
Code: Select all
cout << x; // print x regardless of what type it is. Try this in C.