Re: Packing of structure fields and whole structs

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

 



Phil Endecott writes:
 > Andrew Haley wrote:
 > > gcc should always generate correct code for accessing unaligned data.
 > 
 > but Paul Brook wrote:
 > > You can't reliably take the address of a member of a packed
 > > structure (gcc doesn't have unaligned pointers).
 > 
 > Are you both right?  At first your comments look contradictory, but
 > my slow brain is starting to understand now.
 > 
 > 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?  A quick test seems to indicate that this is indeed 
 > what happens.

Sounds right.  A member of a packed struct must be accessed in there,
where it is unaligned.  

 > But I don't get any error or warning when I do this.  Would you
 > agree that an error (or at least a "big fat warning") would be
 > appropriate at the point where I wrote &f.a?

Not necessarily.  It's perfectly appropriate to do something like:

  memcpy (&f.a, foo, sizeof f.a);

And we don't want to generate bazillions of warnings for that.

Andrew.

[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