This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: size of derived classes - misfeature of gcc?




On Mon, 7 May 2001, Christian Szegedy wrote:

> 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?

I think the reason for this behaviour is highly system dependent !
You can even tune this behaviour sometimes with alignment features
and it may vary by optimization options.
It has to do with alignment. Also the gcc may not honour data structures
without data. Your example is quite esoteric.

CU INGO



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]