🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#824
From: Stefan Haller
Date: Wed, 18 Jun 2003 14:43
52 lines
2275 bytes
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.

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: <1fwrc5z.1wbucc58vha4aN%stk@snafu.de>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!proxad.net!newsfeed.stueberl.de!eusc.inter.net!news.eusc.inter.net!stk

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f635e,98e56c06675247cc,start
X-Google-Attributes: gidf635e,public
X-Google-Thread: 10bcce,98e56c06675247cc,start
X-Google-Attributes: gid10bcce,public
X-Google-ArrivalTime: 2003-06-18 07:43:47 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.icl.net!newsfeed.fjserv.net!proxad.net!newsfeed.stueberl.de!eusc.inter.net!news.eusc.inter.net!stk
From: stk@snafu.de (Stefan Haller)
Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac
Subject: Alignment (padding) of derived classes, Mach-O vs. CFM
Date: Wed, 18 Jun 2003 16:43:30 +0200
Organization: none
Lines: 52
Message-ID: <1fwrc5z.1wbucc58vha4aN%stk@snafu.de>
X-Trace: news.eusc.inter.net 1055947427 23113 213.23.141.164 (18 Jun 2003 14:43:47 GMT)
X-Complaints-To: abuse@eusc.inter.net
Mail-Copies-To: never
User-Agent: MacSOUP/2.5b2 (Mac OS X version 10.2.6)