🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac.programmer.codewarrior
Article #825

Re: Alignment (padding) of derived classes, Mach-O vs. CFM

#825
From: Ed Swartz
Date: Fri, 20 Jun 2003 00:52
73 lines
3620 bytes
"Stefan Haller" <stk@snafu.de> wrote in message
news:1fwrc5z.1wbucc58vha4aN%stk@snafu.de...
> We need to pass objects between a Mach-O application and a CFM shared
> library, built from the same source code.
>
> It seems that the Mach-O and CFM compilers behave differently with
> regard to the alignment of derived classes, so that the object layout is
> different.  Obviously this is a serious problem for us.

Well, even given the specific rules, your problem is deeper than the member
layout.  CFM and Mach-O use completely different ABIs, so the layout of C++
objects is unlikely to correspond between the two (the vtable pointer, for
instance, is in different places).  CFM is based off a 16-bit 68K
architecture, while Mach-O is based off a 32-bit PPC architecture, so there
are inherent unconfigurable differences in the ways the invisible parts of
the classes are aligned.

Not to mention, using any features like RTTI, virtual methods, etc. will
simply not work if you try to pass these objects back and forth between
programs using the two ABIs.

I would recommend using C structs instead.  In this case, the "Mac 68K
2-byte" alignment is probably your best bet.  Beyond that, #pragma pack(1)
... pack(8) would be best for assuring the struct layouts are the same
between the two compilers.

>
> The following sample code illustrates the problem.  I don't understand
> what the padding rules are here; CFM doesn't *always* pad so that the
> derived class starts on the next 8-byte boundary, because if you comment
> out the "int i" in Base, the offset of b2 is 1 on both compilers.
>
> Is this a bug in one of the compilers?  Is there anything we can do to
> work around this problem?  (We could manually pad all our classes to
> multiples of 8 bytes, but this is very error-prone.)
>
> (I have played with the various "#pragma options align=", but it doesn't
>  seem to make a difference.)
>
>
> #include <stdlib.h>  // for offsetof
> #include <assert.h>
>
> class Base
> {
> public:
>   int i;
>   bool b1;
> };
>
> class Derived : public Base
> {
> public:
>   bool b2;
> };
>
> int main(void)
> {
> #ifdef __MACH__
>     assert(offsetof(Derived,b2) == 5);
> #else
>     assert(offsetof(Derived,b2) == 8);
> #endif
>
>     return 0;
> }
>
>
> --
> Stefan Haller
> Berlin, Germany
> http://home.snafu.de/stk/
Technical Details
Message-ID: <bctluk$kn0$1@newshost.mot.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed-east.nntpserver.com!nntpserver.com!newsfeed1.sea.pnap.net!newsfeed.pnap.net!newsgate.mot.com!newshost.mot.com!not-for-mail
References: <1fwrc5z.1wbucc58vha4aN%stk@snafu.de>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10bcce,98e56c06675247cc
X-Google-Attributes: gid10bcce,public
X-Google-Thread: f635e,98e56c06675247cc
X-Google-Attributes: gidf635e,public
X-Google-ArrivalTime: 2003-06-20 00:19:39 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed-east.nntpserver.com!nntpserver.com!newsfeed1.sea.pnap.net!newsfeed.pnap.net!newsgate.mot.com!newshost.mot.com!not-for-mail
From: "Ed Swartz" <eswartz@antispam.metrowerks.com>
Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac
Subject: Re: Alignment (padding) of derived classes, Mach-O vs. CFM
Date: Thu, 19 Jun 2003 19:52:23 -0500
Organization: Motorola
Lines: 75
Message-ID: <bctluk$kn0$1@newshost.mot.com>
References: <1fwrc5z.1wbucc58vha4aN%stk@snafu.de>
NNTP-Posting-Host: behemoth4.mtwk.sps.mot.com
X-Trace: newshost.mot.com 1056070420 21216 10.86.0.44 (20 Jun 2003 00:53:40 GMT)
X-Complaints-To: motpost1@azmsg.mot.com
NNTP-Posting-Date: 20 Jun 2003 00:53:40 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4522.1200