Re: Packing of structure fields and whole structs

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

 



> I think you're saying that I can declare something that is unaligned:
>
>    char c;
>    struct foo f;  // unaligned
>
> and then operate on it locally:
>
>    f.a++;
>
> because gcc knows that f.a is unaligned and on a machine that doesn't
> do unaligned accesses it can generate appropriate byte-shuffling code.
> But if I take its address:
>
>    func(&f.a);
>
> it won't work, because func() assumes that the pointer it is passed is
> aligned. Is this correct?

Almost. Your conclusion is correct, the logic you use to get there is not 
quite right.

The important distinction is whether the access is done through the packed 
struct type. In the former case the compiler sees that we're accessing a 
member of a struct foo (it would still work if f were (struct foo *)). In the 
latter case func() doesn't know anything about struct foo, it only sees that 
type of the pointer, which it assumes is aligned.

This is similar to the aliasing exception for unions: If fields are accessed 
via the union type gcc knows they can alias. If they are accessed directly 
all bets are off.

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