From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17081 invoked by alias); 15 Dec 2001 23:32:28 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 17060 invoked from network); 15 Dec 2001 23:32:28 -0000 Received: from unknown (HELO www.cgsoftware.com) (208.155.65.221) by sources.redhat.com with SMTP; 15 Dec 2001 23:32:28 -0000 Received: from localhost (localhost [127.0.0.1]) by www.cgsoftware.com (8.9.3/8.9.3) with ESMTP id SAA10595; Sat, 15 Dec 2001 18:32:23 -0500 Date: Sat, 15 Dec 2001 16:09:00 -0000 From: Daniel Berlin To: Sam Lantinga cc: Subject: Re: C++ bug in structure packing and inheritance In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-SW-Source: 2001-12/txt/msg00840.txt.bz2 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 > #include > > 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 >