Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > cout bloating file sizes?

cout bloating file sizes?
Thread Tools
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 2, 2003, 10:39 PM
 
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'."
     
Thinine
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jan 6, 2003, 03:51 AM
 
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.
     
Gul Banana
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Jan 6, 2003, 07:04 AM
 
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
     
int69h
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jan 6, 2003, 08:15 PM
 
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::".
     
Thinine
Mac Elite
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jan 7, 2003, 04:39 AM
 
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.
     
int69h
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jan 15, 2003, 04:55 PM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 10:03 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,