From mboxrd@z Thu Jan 1 00:00:00 1970 From: Shaun Jackman To: gcc-help@gcc.gnu.org Subject: packed and aligned structures Date: Wed, 12 Sep 2001 12:58:00 -0000 Message-id: X-SW-Source: 2001-09/msg00054.html I'm sure this is a common question, and I thought I had it solved in 2.95.2, but I've recent upgraded to 3.01 and the problem rears its ugly head again. Here's a basic rundown of what I'm looking for. All my protocol structures must be PACKED (no padding in the structure), EXACT SIZE (no padding at the end of the structure, if the structure is 13 bytes long, sizeof( mystruct) should return 13, not 16), and ALIGNED to a 4 byte boundary. What combination of attributes and command line switches can I use to accomplish this? --- ALIGNED --- In 2.95.2 all my structures always seemed to be on 4 byte boundaries regardless of content, however 3.01 put a structure of this form typedef struct { char pad[2]; char data[ 1400]; } mystruct; on a 2 byte boundary. Will } __attribute__((aligned(4))) mystruct; fix this? Is there a command line switch to force this for all structs? --- EXACT SIZE --- typedef struct { long src, long dst, char mbz, char prot, short len, short checksum } mystruct; sizeof( mystruct) returns 16, how do I make this return 14? --- PACKED --- typedef struct { ... } __attribute__((packed)) mystruct; Will this force all the members of the structure to be packed (ie no padding in the structure)? typedef struct { ... } __attribute__((packed, aligned(4))) mystruct; Does this construct make sense to force the structure to a 4 byte boundary, as well as packing the members? Many thanks, Please cc me as I'm not a member of the list, Shaun