|
|
cout bloating file sizes?
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
I recently noticed something strange about a few Objective-C++ programs I've written, and I finally whittled it down to this: Any program that uses cout or cin will be much larger than I'd expect.
I wrote the following program as a test:
Code:
#include <iostream>
int main(int argc, char *argv[])
{
cout << "Hello, world!" << endl;
return 0;
}
I compile it with c++ -o executablename executablename.cpp.
It compiles and runs fine, but the resulting executable is 120 KB! If I replace the cout with a printf, it drops down to 12 KB. Is this a known bug in the Darwin version of GCC? Or is cout supposed to bloat a program to 10 times the size of the C equivalent, and I just didn't know?
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jul 2002
Status:
Offline
|
|
I'm not sure how that works. When ever I use <iostream> I have to add 'using namespace std;' or it won't compile at all.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
It's not cout, per se, that is doing the bloating. Consider the following two programs:
Code:
#include <iostream.h>
int main() {
printf("Hello, world!\n");
}
Code:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
}
The first version is 352336 bytes; the second is 11572. Run /usr/bin/nm on the binaries to see why.. the output is too verbose to reproduce here, but suffice it to say that anything including a libstdc++ header will include symbol names fro the entire Standard C library, as well as for the library they use - iostreams, for example, is actually rather large. The good news is that this is an O(1) overhead - you may get 300 unnecessary KB from using C++, but you won't get more bloat than that if your program gets larger. There are workarounds, too.
|
[vash:~] banana% killall killall
Terminated
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
Originally posted by Thinine:
I'm not sure how that works. When ever I use <iostream> I have to add 'using namespace std;' or it won't compile at all.
I'm gonna give you the benefit of the doubt and assume that you weren't trolling and are simply naieve.
Obviously he didn't cut and paste this trivial example. Try prefacing cout and endl with "std::".
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jul 2002
Status:
Offline
|
|
I'm not naive, just inexperienced at C++ programming. I was merely wondering how he got the program to work with <iostream> without using std. No need to be a dick.
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
From Merriam-Webster Online:
Main Entry: na�ive
<snip>
2 a : deficient in worldly wisdom or informed judgment;
<snip>
As you can see, naive isn't necessarily a derogatory term. Your use of "dick" is though. Don't expect me to clarify things for you again.
If I was trying to be a dick I would have said, "You stupid newbie, don't you know how namespaces work?!?!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|