Re: [13278] sizeof and allignment on 32bit target

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

 



John Yates wrote:
Long ago I internalized the point that sizeof() does
not return the minimum number of bytes that need to
be copied in an aggregate assignment but rather the
spacing between elements in an array:

struct foo { ... };
foo a[2];

((char*)&a[0]) + sizeof(foo) equals ((char*)&a[1])

This is a bit of a hole in the language definition
since there really is no way to obtain that minimum
number.

struct foo { ... some_type last_member; };

offsetof(struct foo, last_member) + sizeof a[0].last_member;

> Hence using sizeof() as the length of a
memcpy() to set a freestanding risks trashing some
other object if the compiler has practiced any kind
storage packing.

Overwriting padding bytes is guaranteed to be safe.



Segher




[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