From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa528,9af04b2baa5a026a X-Google-Attributes: gidfa528,public X-Google-ArrivalTime: 2003-03-21 12:48:10 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: marc.gibian@acm.org (Marc S. Gibian) Newsgroups: comp.unix.tru64 Subject: Re: Data structure alignment question Date: 21 Mar 2003 12:48:09 -0800 Organization: http://groups.google.com/ Message-ID: <8df11a0.0303211248.6d44bd12@posting.google.com> References: <8df11a0.0303191636.389f8f0c@posting.google.com> NNTP-Posting-Host: 209.20.248.219 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1048279689 6720 127.0.0.1 (21 Mar 2003 20:48:09 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 21 Mar 2003 20:48:09 GMT Xref: archiver1.google.com comp.unix.tru64:9746 David Konerding wrote in message news:... > The "info" pages, which are included with the gcc distribution, have > a page on this, "Variable attributes" which explain how to specify the > alignment and packing of structures. I have attempted to follow these instructions and things still just did not seem to work... see below for more details. > However, your message seems to suggest that you are writing the binary > data of a structure directly over the network. Is that correct? If so, > you should be aware many people consider this a poor solution because > it's not very portable. I always use some intermediate data encoding > which ensures portable behavior (in my case CORBA, but other data > encoders exist). Rather than get into the merits of binary vs. text based protocols, this is not an open issue for my current effort because I am porting software that has been around for quite a while, with a significant installed base. Thus, we need to avoid changing the protocol to maintain compatibility with the existing installed product. One example of the structures that I am working with are: class base { long one; unsigned short two; }; class msg : public base { char three[size]; }; When used on Tru64, three is offset 6 bytes from the start of the msg class. For all other platforms this software runs on, three is offset eight bytes from the start. I have tried: class base { long one __attribute__ ((aligned(4))); unsigned short two __attribute__ ((aligned(4))); } __attribute__ ((aligned(4))); class msg : public base { char three[size] __attribute__ ((aligned(4))); } __attribute__ ((aligned(4))); and get the same result. I have tried just about every permutation I can think of and continue to get exactly the same result. I originally thought I would have to pack things on Tru64 since its natural alignment is 8 byte boundaries. But, the actual results I am seeing are that these classes are already being packed more tightly than I want, so that seems the wrong angle to pursue. The only solution I've found thus far is to manually pad as: class base { long one; unsigned short two; short pad; } This works just fine, though it means I have to manually go through all of my classes and check their alignment on Tru64, not a trivial task. Thanks again for your help with this. -Marc