public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Double abstract class Inheritance concern.
@ 2004-03-29 14:03 Chris Lattner
  2004-03-29 17:16 ` Nathan Sidwell
  0 siblings, 1 reply; 5+ messages in thread
From: Chris Lattner @ 2004-03-29 14:03 UTC (permalink / raw)
  To: Tiago Stein; +Cc: gcc


Tiago Stein wrote:
> class A{};
> class B :public A{};
> class C: public A{ int g; };
> class D: public B, public C {
>   unsigned int foo;
> };
> Why sizeof class D is 12? Should not be 8?

I believe the reason is that the ABI does not permit multiple different
instances of A to be located at the same address, even if they have zero
size.  If the size of the structure was 8, all of "A", "B", and "A" would
have offset zero.

I'm not sure WHY this is required, but this is the reason it happens at
least.  A declaration of "A X[100];" allocates 100 bytes as
a result of the same rule.

-Chris

-- 
http://llvm.cs.uiuc.edu/
http://www.nondot.org/~sabre/Projects/

^ permalink raw reply	[flat|nested] 5+ messages in thread
* Double abstract class Inheritance concern.
@ 2004-03-29 13:57 Tiago Stein
  2004-03-29 15:15 ` Alexandre Oliva
  0 siblings, 1 reply; 5+ messages in thread
From: Tiago Stein @ 2004-03-29 13:57 UTC (permalink / raw)
  To: gcc

Hy! I have  question concerning inheritance of empty classes.

Supose that I have:

Class A{

};

Class B: public A{
  unsigned int foo1;
};

because of C++ constraints on object sizes.. sizeof(A) is  4 (I am in a
32bit PC).


In class B, due to the attribute.. this extra size form A is supressed. So
sizeof B= 4 too..


Until here everything is OK, but... given:


class A{
};

class B :public A{
};


class C: public A{
 int g;
};

class D: public B, public C
{
  unsigned int foo;
};


Why sizeof class D is 12? Should not be 8? Why the extra size from the
empty classes is not eliminated this time? Is this due to any rules in the
standard? I passed hours looking for it but found nothing that could
obligate so.  This behavior is causing some real nasty side-effects in our
operating system due to memory restrictions in our embedded targets.


So, can someone explain me why of this? Maybe I am leaving something wrong
pass at my design if there is any rule that dictates this behavior.

Thanks for any help.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-03-29 16:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-29 14:03 Double abstract class Inheritance concern Chris Lattner
2004-03-29 17:16 ` Nathan Sidwell
2004-03-29 18:49   ` Joe Buck
  -- strict thread matches above, loose matches on Subject: below --
2004-03-29 13:57 Tiago Stein
2004-03-29 15:15 ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).