473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Empty program so large?

When I compile the simplest possible c++ program:

int main ( int , char** ) { return 0 ; }

using gcc 2.95 on OS X the executable is about 9.5 kilobytes. Using
gcc 3.2 it rocks in around 10 kilobytes. By the way, I used -O3 with
no debugging symbols or profiling code.

Now, I'm no compiler or assembler expert and this question is pure
curiosity. Why is this empty program so large? Roughly speaking, what
are those two thousand or so machine instructions for? What file sizes
do you get with your compiler/platform?

I've seen Hello World programs in PowerPC assembly that are less than
20 instructions. Does this empty program really need to be one hundred
times larger?
Jul 22 '05 #1
6 1235
Keith H Duggar wrote:
When I compile the simplest possible c++ program:

int main ( int , char** ) { return 0 ; }

using gcc 2.95 on OS X the executable is about 9.5 kilobytes. Using
gcc 3.2 it rocks in around 10 kilobytes. By the way, I used -O3 with
no debugging symbols or profiling code.

Now, I'm no compiler or assembler expert and this question is pure
curiosity. Why is this empty program so large? Roughly speaking, what
are those two thousand or so machine instructions for? What file sizes
do you get with your compiler/platform?

I've seen Hello World programs in PowerPC assembly that are less than
20 instructions. Does this empty program really need to be one hundred
times larger?


It's boilerplate startup code, library code linked in by default etc. --
all of which is decidedly off topic here.

<OT>
When you compile, add whatever flag your compiler uses for `verbose';
all the stuff the compiler goes through (or is gone through on its
behalf) to create an executable will be enlightening.
</OT>

Cheers,
--ag

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Jul 22 '05 #2
Keith H Duggar wrote:
When I compile the simplest possible c++ program:

int main(int argc, char* argv[]) { return 0; }

using gcc 2.95 on OS X the executable is about 9.5 kilobytes.
Using gcc 3.2 it rocks in around 10 kilobytes.
By the way, I used -O3 with no debugging symbols or profiling code. cat smallest.cc int main(int argc, char* argv[]) { return 0; }
g++ -Wall -ansi -pedantic -Os -s -o smallest smallest.cc
./smallest
ls -l smallest -rwxr-xr-x 1 edwin hpcc 2692 Feb 13 21:46 smallest g++ --version g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
Now, I'm no compiler or assembler expert
and this question is pure curiosity.
Why is this empty program so large? Roughly speaking,
what are those two thousand or so machine instructions for? man gcc

Jul 22 '05 #3
Keith H Duggar wrote:
When I compile the simplest possible c++ program:

int main ( int , char** ) { return 0 ; }

using gcc 2.95 on OS X the executable is about 9.5 kilobytes. Using
gcc 3.2 it rocks in around 10 kilobytes. By the way, I used -O3 with
no debugging symbols or profiling code.


The FAQ covers this under the topic "Why is my executable so large?"

http://www.parashift.com/c++-faq-lit....html#faq-36.8

Checking the FAQ before posting is always good.

But the FAQ doesn't say that implementations balance their libraries to
reduce the size of large apps, not small apps.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #4
> But the FAQ doesn't say that implementations balance their libraries to
reduce the size of large apps, not small apps.


Maybe so but then GCC and others seem to completely diregard the "pay
for what you use" design principle of C++.

In fact, in many threads (not necessarily C/C++ related) I have found
people stating that such behavior is :

"result of sloppy linker"
"a bad compiler"
"(not) a decent optimizing compiler"

Thus, would you say that those statements apply to GCC and others?

Doesn't this also make it rather impratical to write tight utilities
using these C++ compilers? Do you know of any way to force them to
write better code for small utility programs? Or any alternative C++
compilers that are a little more careful?

Many others posters constantly tout the "excellent optimizing
compilers" and say "don't bother with hand optimization the compiler
will do much better that you ever could" etc. These statements do not
seem to be justified considering this example as well as many others
discussed in the forum regarding various C++ implementations.
Jul 22 '05 #5
du****@mit.edu (Keith H Duggar) wrote in message news:<b4*************************@posting.google.c om>...
When I compile the simplest possible c++ program:

int main ( int , char** ) { return 0 ; }

using gcc 2.95 on OS X the executable is about 9.5 kilobytes. Using
gcc 3.2 it rocks in around 10 kilobytes. By the way, I used -O3 with
no debugging symbols or profiling code.

Now, I'm no compiler or assembler expert and this question is pure
curiosity. Why is this empty program so large? Roughly speaking, what
are those two thousand or so machine instructions for? What file sizes
do you get with your compiler/platform?

I've seen Hello World programs in PowerPC assembly that are less than
20 instructions. Does this empty program really need to be one hundred
times larger?


IIRC when I tried writing hello world on a Sun computer back in the
1980s, it came to 300Kbytes, and a simple graphical game of life came
to 600Kbytes (which would not even have fitted into memory of the PCs
of the time) so you are getting off lightly!
Jul 22 '05 #6
Keith H Duggar wrote:
But the FAQ doesn't say that implementations balance their libraries to
reduce the size of large apps, not small apps.
Maybe so but then GCC and others seem to completely diregard the "pay
for what you use" design principle of C++.

In fact, in many threads (not necessarily C/C++ related) I have found
people stating that such behavior is :

"result of sloppy linker"
"a bad compiler"
"(not) a decent optimizing compiler"

Thus, would you say that those statements apply to GCC and others?


No.
In real life it is very uncommon that you write a program just like the
one you presented.

Doesn't this also make it rather impratical to write tight utilities
using these C++ compilers?


Don't think so. Even in small utilities you need at least
a few things: startup code, I/O facilities, termination code, probably
some dynamic memory allocations.

So even if the 'smallest program' takes some size, you will find that
this size does increase slowly as you add code to your program.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Emmanuel Thomé | last post by:
This is a comment aside the empty class behavior FAQ. I understand there are a fair number of reasons which make empty classes have non-zero size (except as base classes). If ``class foo'' were...
2
by: William | last post by:
I have declared the following struct: struct PropagateInfo { string type; // "registration", "gossip", "termination" int initiatePeerID; }; which I used as follows: PropagateInfo*...
13
by: could ildg | last post by:
I want to check if a folder named "foldername" is empty. I use os.listdir(foldername)== to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do...
7
by: Matthew Wieder | last post by:
Hi - I have a datagrid that has a black header and the rows alternate white and gray. The problem is that until items are added to the grid, the grid appears as a large black rectangle, which is...
22
by: EMW | last post by:
Hi, I managed to create a SQL server database and a table in it. The table is empty and that brings me to my next chalenge: How can I get the info in the table in the dataset to go in an empty...
8
by: meendar | last post by:
what will a object of an Empty class( contain nothing), do on default.What are all the default methods it calls. what is the use of creating the object for an empty class?
12
by: robertino | last post by:
Hi all, I've put together a few SPs to produce a BOM (bill of materials) listing, which together use a couple of global temp tables, and return the results from a cursor. Here's the code: ...
5
by: Per Juul Larsen | last post by:
Hi. My application creates empty libraries. How do I ensure that the user copies at least one or more picture files (.jpg) in each of the empty folders ? Result , no empty folders! regards pjl
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.