size of derived classes - misfeature of gcc?

Christian Szegedy szegedy@or.uni-bonn.de
Mon May 7 06:45:00 GMT 2001


Hi!

I've got a question regarding the size and type management of gcc.
Suppose that I ompile the follwoing programm using gcc 2.95.2 or 2.95.3:

-------------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>

class A
{
public:
};

class B : public A
{
public:
   int i;
};

class C
{
public:
   int i;
};


void main()
{
   printf("%u %u %u\n",(unsigned)sizeof(A),
	                    (unsigned)sizeof(B),
                       (unsigned)sizeof(C));
}

-------------------------------------------------------------------------------

Then, I would except that C and B have both size 4, and therefore the output
should be:
1 4 4

The result of this run is:
1 8 4 
for some reason I don't understand.

Other compilers (e.g. xlC under AIX) produce the expected output above.
Of course this is no bug, since the generated code runs without errors.
But it can pose a significant memory overhead in practical cases in which
A has some static functions and data which is shared by several classes which
are small, but have a lot of instances (the case I actually have).

Is there some important reason for gcc to behave in this way?

Are the future versions of gcc going to have this misfeature?




More information about the Gcc-help mailing list