On Tuesday 05 December 2006 22:40, Phil Endecott wrote:
> Dear GCC Experts,
>
> I am trying to understand the subtleties of __attribute__((packed)). I
> have some code that works on x86, where unaligned accesses work, but
> fails on ARM where they do not.
>
> As far as I can see, if I declare a struct with the packed attribute
> applied to the whole struct, like this:
>
> struct test {
> int a;
> int b;
> } __attribute__((packed));
>
> and then use it like this:
>
> {
> char c;
> struct test t;
> }
>
> Then t will be packed next to c, and t.a and t.b will be unaligned.
> This is not what I want!
The compiler did exactly what you told it to. Don't use
__attribute__((packed)), or also specify a larger
alignment__attribute__((aligned(N))).
You can't reliably take the address of a member of a packed structure (gcc
doesn't have unaligned pointers).
Paul