__u32 (linux/types.h) is 4-byte aligned (__u32-aligned) - that's
certain!
But: Is a struct aligned according to its size, or according to the
largest component it contains:
Example:
What about a struct that has a sizeof 4 bytes, but contains at most a
__u16:
Will it be __u16 aligned? Or rather: will it be __u32 aligned since
the whole structure has 4 bytes?
But then also: What if a struct contains a __u8-array of say 4
elements?
What if sizeof(struct abc) is 6??
etc.
There are code-snippets below (that will hopefully make it easier to
discuss):
#include <linux/types.h>
/***************** (1a) ******************/
struct mystruct1a {
__u8 a;
__u16 b;
__u8 c;
};
struct mystruct1a ms1a;
/*
### will the whole struct be __u8-aligned, or __u16-aligned, or __u32-
aligned ??
*/
/***************** (1b-packed) ***********/
struct mystruct1b {
__u8 a;
__u16 b;
__u8 c;
} __attribute__ ((packed));
struct mystruct1b ms1b;
/*
### whole struct: __u8-aligned, or __u16-aligned, or __u32-aligned ??
*/
/***************** (2a) ******************/
struct mystruct2a {
__u8 arr[3];
};
struct mystruct2a ms2a;
/*
### whole struct: __u8-aligned, or __u16-aligned, or __u32-aligned ??
would a union behave differently?
*/
/***************** (2b-packed) ***********/
struct mystruct2b {
__u8 arr[3];
} __attribute__ ((packed));
struct mystruct2b ms2b;
/*
### whole struct: __u8-aligned, or __u16-aligned, or __u32-aligned ?
would a union behave differently?
*/
/***************** (3a) ******************/
struct mystruct3a {
__u8 arr[4];
};
struct mystruct3a ms3a;
/*
### whole struct: __u8-aligned, or __u16-aligned, or __u32-aligned ??
would a union behave differently?
*/
/***************** (3b-packed) ***********/
struct mystruct3b {
__u8 arr[4];
} __attribute__ ((packed));
struct mystruct3b ms3b;
/*### whole struct: __u8-aligned, or __u16-aligned, or __u32-
aligned ??
would a union behave differently?
*/
Also: Does gcc have a way of allowing the programmer to flexibly
control such alignments?
Thanks
-Albert
According to a multiple of " the least common multiple of its
elements' alignment requirements":
An answer (from Eric Sosman) to the questions concerning non-packed
structs can be seen here:
http://groups.google.com/group/comp.lang.c/msg/9e69e689975bc07e?
> Example:
> What about a struct that has a sizeof 4 bytes, but contains at most a
> __u16:
> Will it be __u16 aligned? Or rather: will it be __u32 aligned since
> the whole structure has 4 bytes?
Consider it __u16-aligned, to go save! could also be a multiple of
__u16, e.g. __u32
> But then also: What if a struct contains a __u8-array of say 4
> elements?
__u8 aligned to go save!
> /***************** (1a) ******************/
> struct mystruct1a {
> __u8 a;
> __u16 b;
> __u8 c;
>
> };
>
> struct mystruct1a ms1a;
> /*
> ### will the whole struct be __u8-aligned, or __u16-aligned, or __u32-
> aligned ??
> */
__u16 to go save!
>
> /***************** (1b-packed) ***********/
> struct mystruct1b {
> __u8 a;
> __u16 b;
> __u8 c;
>
> } __attribute__ ((packed));
>
> struct mystruct1b ms1b;
> /*
> ### whole struct: __u8-aligned, or __u16-aligned, or __u32-aligned ??
> */
>
???
> /***************** (2a) ******************/
> struct mystruct2a {
> __u8 arr[3];
>
> };
>
> struct mystruct2a ms2a;
> /*
> ### whole struct: __u8-aligned, or __u16-aligned, or __u32-aligned ??
> would a union behave differently?
> */
>
__u8 to go save
> /***************** (2b-packed) ***********/
> struct mystruct2b {
> __u8 arr[3];
>
> } __attribute__ ((packed));
>
> struct mystruct2b ms2b;
> /*
> ### whole struct: __u8-aligned, or __u16-aligned, or __u32-aligned ?
> would a union behave differently?
> */
>
???
> /***************** (3a) ******************/
> struct mystruct3a {
> __u8 arr[4];
>
> };
>
> struct mystruct3a ms3a;
> /*
> ### whole struct: __u8-aligned, or __u16-aligned, or __u32-aligned ??
> would a union behave differently?
> */
>
__u8 to go save!
> /***************** (3b-packed) ***********/
> struct mystruct3b {
> __u8 arr[4];
>
> } __attribute__ ((packed));
>
> struct mystruct3b ms3b;
> /*### whole struct: __u8-aligned, or __u16-aligned, or __u32-
> aligned ??
> would a union behave differently?
> */
>
???
> Also: Does gcc have a way of allowing the programmer to flexibly
> control such alignments?
???
-----------------------------------
Here's something interesting:
Consider a packed struct:
struct structi {
short a; //2 bytes
int b; //4bytes
short c; //2bytes
} __attribute__ ((packed));
struct structi run_this_by_me_again;
Depending on how this is accessed
e.g.
*((int *)(((short*)&run_this_by_me_again)+1))
!!
it might be useful to have this struct aligned at addresses x for
which:
x%4 = 2
x mod 4 = 2
Can this be achieved with gcc ??
-Albert