Re: Packing of structure fields and whole structs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wednesday 06 December 2006 16:48, Phil Endecott wrote:
> Let me make this concrete again.  With this test program:
>
> struct test {
>    int a __attribute__((packed));
>    int b __attribute__((packed));
> };
>
> char c = 1;
> struct test t = { .a=2, .b=3 };
>
> what can I assume about the alignment of t.a?  As I now understand it,
> t.a and t.b both have alignment of 1, so t has alignment of 1, so t can
> be packed immediately after c with no gap.  It is then unsafe (on some
> hardware) to take &t.a and pass it to a function that wants an int*.

Correct. It may be unsafe on all hardware because the compiler is allowed to 
assume that the low 2 bits of an int* are zero.

> On the other hand, on ARM it leaves a gap:

> struct test {
>    int a;
>    int b;
> } __attribute__((packed));
>
> then the ARM compiler generates the same layout as the x86 compiler,
> without the gap.

This is because the ARM ABI you're using specifies that all structures have a 
minimum alignment of 4 bytes. ie.
struct foo {
 char c;
};
has size and alignmet 4.

Making the structure packed overrides that.

Paul

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux