Purpose
I am writing a network program in C (specifically gnu89
) and I would like to simplify things by reinterpreting a certain struct X
as big array of bytes (a.k.a. char
), sending the bytes over the network, and reinterpreting them as struct X
on the other side. To this end I have decided to use gcc's __attribute__((__packed__ )). I have done my best to ensure that this is done correctly (i.e. I've accounted for endianness and other related issues).
Question
Other than guaranteeing that struct X
is as small as possible, does gcc guarantee that a struct
defined with __attribute__((__packed__ )) retains the original ordering? I've done a fair amount of searching and I have yet to find any documentation on whether or not this guarantee exists.
Notes
It is safe to assume that both the sender and receiver will encounter no portability issues (e.g. sizeof(int)
on the server is equal to sizeof(int)
on the client).