public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* 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

* Re: Double abstract class Inheritance concern.
  2004-03-29 13:57 Double abstract class Inheritance concern Tiago Stein
@ 2004-03-29 15:15 ` Alexandre Oliva
  0 siblings, 0 replies; 5+ messages in thread
From: Alexandre Oliva @ 2004-03-29 15:15 UTC (permalink / raw)
  To: Tiago Stein; +Cc: gcc

On Mar 28, 2004, Tiago Stein <tiago@lisha.ufsc.br> 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? Why the extra size from the
> empty classes is not eliminated this time? Is this due to any rules in the
> standard?

D::B::A and D::C::A must have different addresses.

-- 
Alexandre Oliva             http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist  oliva@{lsd.ic.unicamp.br, gnu.org}

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

* Re: Double abstract class Inheritance concern.
  2004-03-29 17:16 ` Nathan Sidwell
@ 2004-03-29 18:49   ` Joe Buck
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Buck @ 2004-03-29 18:49 UTC (permalink / raw)
  To: Nathan Sidwell; +Cc: Chris Lattner, Tiago Stein, gcc


Chris Lattner wrote:
> > 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.

On Mon, Mar 29, 2004 at 08:57:17AM +0100, Nathan Sidwell wrote:
> It is a C++ language requirement that no two logically distinct objects
> of the same type have the same address.

However, g++ still finds plenty of opportunities to allocate zero bytes
for a struct/class object, when it is not adjacent to any other objects of the
same type (e.g. an empty base class, or an empty data member of a struct
or class).


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

* Re: Double abstract class Inheritance concern.
  2004-03-29 14:03 Chris Lattner
@ 2004-03-29 17:16 ` Nathan Sidwell
  2004-03-29 18:49   ` Joe Buck
  0 siblings, 1 reply; 5+ messages in thread
From: Nathan Sidwell @ 2004-03-29 17:16 UTC (permalink / raw)
  To: Chris Lattner; +Cc: Tiago Stein, gcc

Chris Lattner wrote:
> 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.

It is a C++ language requirement that no two logically distinct objects
of the same type have the same address.

BEWARE. GNU C has an empty struct extension to C, that does not obey
the same rules.

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk


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

* 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

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 13:57 Double abstract class Inheritance concern Tiago Stein
2004-03-29 15:15 ` Alexandre Oliva
2004-03-29 14:03 Chris Lattner
2004-03-29 17:16 ` Nathan Sidwell
2004-03-29 18:49   ` Joe Buck

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).