data alignment

Peeter Joot/Toronto/IBM peeterj@ca.ibm.com
Wed Sep 13 21:24:00 GMT 2000


>     I've used "__attribute__ ((packed))" after defining a struct, but it
> decreases portability.

Hi Silvio,

There is no such thing as "decreases portability" if you are delving into
the use of pragma's for packing or using __attribute__((packed)).  This is
an inherently unportable action, and if you do it you have to face the
consequences.

In fact it is a really really bad idea in general and will cause exceptions
(or real poor performance) on a number of platforms when the unaligned
members are accessed.  Unless you have a really good reason, just don't do
it.

Perhaps you can reorder your structure to fix the alignment problems.  What
specific reason do you need a packed structure?  Another options is
treating the memory in a raw format and doing bytewise copies in and out of
the structure -- that is probably as close to portable you are going to get
(if you are careful).

Peeter




More information about the Gcc mailing list