packed and aligned structures

Shaun Jackman sjackman@pathwayconnect.com
Wed Sep 12 13:18:00 GMT 2001


On the matter of exact structure size. I forget to mention I've already tried 
specifying -mstructure-size-boundary=8, which had the desired with 2.95.2. 
This doesn't seem to work with 3.0.1 though. Is it still a supported option? 

Thanks,
Shaun

On September 12, 2001 13:58, you wrote:
> 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



More information about the Gcc-help mailing list