public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: size of derived classes - misfeature of gcc?
@ 2001-05-07 10:12 Mike Stump
  0 siblings, 0 replies; 4+ messages in thread
From: Mike Stump @ 2001-05-07 10:12 UTC (permalink / raw)
  To: gcc-help, gcc, szegedy

> Date: Mon, 7 May 2001 15:45:11 +0200
> From: Christian Szegedy <szegedy@or.uni-bonn.de>
> To: gcc-help@gcc.gnu.org, gcc@gcc.gnu.org

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

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

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

Backwards compatibility is the main reason.

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

No.  The upcoming gcc 3.0 I think will print 1 4 4 as I recall.

Please don't post to gcc-help and gcc at the same time, thanks.

Hope this helps.  If you'd like more background information, search on
google/dejanews for empty base class optimization, I'm sure lots of
folks have said lots about it already.

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

* Re: size of derived classes - misfeature of gcc?
  2001-05-07  6:45 Christian Szegedy
  2001-05-07  7:00 ` Jakub Jelinek
@ 2001-05-07 11:17 ` Ingo Krabbe
  1 sibling, 0 replies; 4+ messages in thread
From: Ingo Krabbe @ 2001-05-07 11:17 UTC (permalink / raw)
  To: Christian Szegedy; +Cc: gcc-help, 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


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

* Re: size of derived classes - misfeature of gcc?
  2001-05-07  6:45 Christian Szegedy
@ 2001-05-07  7:00 ` Jakub Jelinek
  2001-05-07 11:17 ` Ingo Krabbe
  1 sibling, 0 replies; 4+ messages in thread
From: Jakub Jelinek @ 2001-05-07  7:00 UTC (permalink / raw)
  To: Christian Szegedy; +Cc: gcc-help, gcc

On Mon, May 07, 2001 at 03:45:11PM +0200, Christian Szegedy wrote:
> 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.

In the new g++ ABI this gives 1 4 4.

	Jakub

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

* size of derived classes - misfeature of gcc?
@ 2001-05-07  6:45 Christian Szegedy
  2001-05-07  7:00 ` Jakub Jelinek
  2001-05-07 11:17 ` Ingo Krabbe
  0 siblings, 2 replies; 4+ messages in thread
From: Christian Szegedy @ 2001-05-07  6:45 UTC (permalink / raw)
  To: gcc-help, gcc

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?


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

end of thread, other threads:[~2001-05-07 11:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-07 10:12 size of derived classes - misfeature of gcc? Mike Stump
  -- strict thread matches above, loose matches on Subject: below --
2001-05-07  6:45 Christian Szegedy
2001-05-07  7:00 ` Jakub Jelinek
2001-05-07 11:17 ` Ingo Krabbe

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