Topic: c++ code bloat and class declarations


Author: Scott Curry <sxc@mediaone.net>
Date: 1998/04/22
Raw View

(detailed and long question).....

I have a question about header files causing bloated object and
executable files.  Way back in 1992 when I was using the GNU g++
compiler, a coworker of mine went through our code with the following
script:

nm *.o | grep ' t ' | awk '{ print $3 }' | sort | uniq -c | sort -nr

looking for duplicate symbols and then based on this output, went
through the source code and forward referenced classes instead of
including them. Also, making the appropriate change of members from
objects to references or pointers.  This greatly reduced overall size of
object and executable file sizes.

So for example, if you had the following header file:

//---------Address.h

#ifndef ADDRESS_H
#define ADDRESS_H

#include <string.h>
class Address
{
public:
    Address();
private:
    String firstName;
    String lastName;
    String street;
    String city;
    String state;
    String zip;

};

#endif // String_H

And then include address.h in another class such as Person, class Person
size would include the size of the six string objects from address as
well.  And if you continue to compose other classes from address,  your
executable size would grow.   But if you declared the strings in the
Address class as references, you would not get the same bloated size. So
far so good.

Let me note a few things:
1) in Address, the string members (firstName,etc.) are declared as
objects of type String and therefore take up the size of String.
2) if Address declared pointers to strings, then the size would only be
the size of a pointer for each member.
3) the string declarations are ALSO definitions; hence, Address needs to
reserve the space for each object.
4) the default constructor for String should be called with each address
member (i.e. firstName,etc.).
5) apparently good linkers in modern PC compilers can optimize the size
of the object or executable files, no matter which way you declare the
strings in address.

So having said all this here is the question(s) I have:

What does the declaration of "String firstName" in address actually do
in terms of memory storage?
Is it then duplicated in all the other object files that reference an
address?
Why did (does) this cause bloated executables in the GNU g++ compiler
(1992ish)?

Obviously, I am missing something fundamental about this problem and I
guess I'm lost.  Another colleague stated that there is no reason for
the code to ever bloat the executable size.  I distinctly remember this
problem back in 1992.  What is the missing link?

In advance, thank you for the help.  If you need further clarification
or information please let me know.  I would appreciate an emailed
response as I do not have the time to sift through all of the c.s.c++
messages

Cheers,
Scott


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]