From: marc.gibian@acm.org (Marc S. Gibian)
Subject: Re: Data structure alignment question
Date: 21 Mar 2003 12:48:09 -0800 [thread overview]
Message-ID: <8df11a0.0303211248.6d44bd12@posting.google.com> (raw)
In-Reply-To: slrnb7k7on.qv.dek@wuzzy.berkeley.edu
David Konerding <dek@compbio.berkeley.edu> wrote in message news:<slrnb7k7on.qv.dek@wuzzy.berkeley.edu>...
> 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
next prev parent reply other threads:[~2003-03-21 20:48 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-03-20 0:36 Data structure alignment question Marc S. Gibian
2003-03-20 16:59 ` Brian Haley
2003-03-20 19:55 ` David Konerding
2003-03-21 20:48 ` Marc S. Gibian [this message]
2003-03-21 23:21 ` David Konerding
2003-03-24 19:49 ` Marc S. Gibian
2003-04-16 1:05 ` Marc S. Gibian
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox