FidoNet Echomail Archive
aust_c_here

<<< Previous Index Next >>>

From: David Nugent
To: Hamish Moffatt
Date: 1997-04-10 01:15:18
Subject: Auto-packing in gcc?

 > The only way I could find to turn off this behaviour was
 > to change the struct definition to say something like
~
 > } __attribute__ ((packed));

 > which isn't very convenient, since I didn't want to
 > modify the structures as given. Is there a command line
 > switch?

No.

Actually, there are some environments where packing structs is never an
option. The only way to write portable code is to use block based I/O and
decoding "structs" byte by byte (or just plain byte by byte I/O,
which is usually woefully inefficient).

In fact, I don't think you'd want to pack structures by default anyway.
You'll find that 90% of the C runtime library that uses structs will cease
to work.


 > (I found a way around it, but it's a bit messy. The
 > structures in question are
 > the Bluewave packet format, and you can #define
 > BIG_ENDIAN to have it make all the multibyte types into
 > arrays of bytes instead, so I just wrote functions to put
 > things in in these types, so no alignment problems.
 > (Despite defining BIG_ENDIAN there is no automatic
 > conversion.))

.. or pack/unpack it into an array of unsigned char. Sounds long-winded and
less convenient - and it is - but then your code is likely to work on just
about any platform.


 > typedef STRUCT_NAME PACKED_STRUCT_NAME __attribute__ ((packed));
 > didn't work.

Of course. The attribute applies to the structure, not the type name.
'typedef' in C is pretty much a misnomer. It doesn't actually create a new
type (although it does in C++), but an alias for the original.

Regards,
David

--- MaltEd/2 1.0.b6
 * Origin: Unique Computing Pty Limited (3:632/348)
SEEN-BY: 633/267 270
@PATH: 632/348 360 50/99 635/728 633/267



<<< Previous Index Next >>>