From: Daniel Berlin <dan@cgsoftware.com>
To: Sam Lantinga <slouken@devolution.com>
Cc: <gcc@gcc.gnu.org>
Subject: Re: C++ bug in structure packing and inheritance
Date: Sat, 15 Dec 2001 16:09:00 -0000 [thread overview]
Message-ID: <Pine.LNX.4.33.0112151807180.7153-100000@www.cgsoftware.com> (raw)
In-Reply-To: <E16FNqC-0007Qg-00@twomix.devolution.com>
On Sat, 15 Dec 2001, Sam Lantinga wrote:
> There seems to be a bug in structure packing in that if you have
> two classes or structures that both inherit from a class with no
> data members, then they will be the wrong size.
> A simple example follows:
>
> #include <stddef.h>
> #include <stdio.h>
>
> struct a {
> };
>
> struct b : public a {
> float x;
> float y;
> };
>
> struct c : public a {
> b data;
> float z;
> };
>
> main()
> {
> printf("size of a = %d (should be 0)\n", sizeof(a));
> printf("size of b = %d (should be 8)\n", sizeof(b));
> printf("size of c = %d (should be 12)\n", sizeof(c));
> printf("offset of c.z = %d (should be 8)\n", offsetof(c,z));
> }
>
> Sample output with the current CVS of gcc is:
> size of a = 1 (should be 0)
> size of b = 8 (should be 8)
> size of c = 16 (should be 12)
> offset of c.z = 12 (should be 8)
I have no idea whether it's correct, but i can tell you *why* it happens.
-fclass-hierarchy-dump shows:
Class a
size=1 align=1
a (0x3015d500) 0 empty
Class b
size=8 align=4
b (0x3015d7c0) 0
a (0x3015d800) 0 empty
Class c
size=16 align=4
c (0x3015db00) 0
a (0x3015db40) 0 empty
If you force it to pack them (__attribute__ packed or whatever), you'll
get:
size of a = 1 (should be 0)
size of b = 8 (should be 8)
size of c = 13 (should be 12)
offset of c.z = 9 (should be 8)
And the class-hierarchy-dump will show:
Class a
size=1 align=1
a (0x3015d500) 0 empty
Class b
size=8 align=1
b (0x3015d800) 0
a (0x3015d840) 0 empty
Class c
size=13 align=1
c (0x3015db40) 0
a (0x3015db80) 0 empty
I dunno whether the empty base class should have size=0 align=0, but if
it did, this should cause the others to do what you expect.
>
> If this is correct, I would appreciate it if somebody could explain it.
>
> Thanks,
> -Sam Lantinga, Software Engineer, Blizzard Entertainment
>
next prev parent reply other threads:[~2001-12-15 23:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-12-15 15:07 Sam Lantinga
2001-12-15 16:09 ` Daniel Berlin [this message]
2001-12-15 23:40 Sam Lantinga
2001-12-15 23:57 ` Alexandre Oliva
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.33.0112151807180.7153-100000@www.cgsoftware.com \
--to=dan@cgsoftware.com \
--cc=gcc@gcc.gnu.org \
--cc=slouken@devolution.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).