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 15:21:46 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!ucberkeley!agate.berkeley.edu!agate!not-for-mail From: David Konerding Newsgroups: comp.unix.tru64 Subject: Re: Data structure alignment question Date: Fri, 21 Mar 2003 23:21:44 +0000 (UTC) Organization: University of California, Berkeley Message-ID: References: <8df11a0.0303191636.389f8f0c@posting.google.com> <8df11a0.0303211248.6d44bd12@posting.google.com> Reply-To: dek@compbio.berkeley.edu NNTP-Posting-Host: wuzzy.berkeley.edu X-Trace: agate.berkeley.edu 1048288904 12613 128.32.236.47 (21 Mar 2003 23:21:44 GMT) X-Complaints-To: usenet@agate.berkeley.edu NNTP-Posting-Date: Fri, 21 Mar 2003 23:21:44 +0000 (UTC) User-Agent: slrn/0.9.7.4 (Linux) Xref: archiver1.google.com comp.unix.tru64:9747 In article <8df11a0.0303211248.6d44bd12@posting.google.com>, Marc S. Gibian wrote: > 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. I appreciate your need to maintain protocol compatibility. However, my point was to avoid taking a struct, and writing it over the network directly. Attempting to get a struct to have the alignment of fields you need by using compiler options is iffy. Even if you don't use a platform-neutral data encoder like XDR or CORBA, you can achieve layout of fields in memory without using structs- you have to do the work by hand, but this is more reliable and portable. Effectively, you end up writing a function which "packs" a struct into a binary representation that is equivalent to your protocol format, and another function which "unpacks" a struct. Is source compatibility still a requirement? You would have to rewrite some core routines to handle this. Dave