
On 6/3/06, Sebastian Redl <sebastian.redl@getdesigned.at> wrote:
Thorsten Ottosen wrote:
1. have I misunderstood aligmnet completely when I thought that a single data member was not guaranteed to be aligned, but an array ways? (your aligned types use a data member, your unaligned use a char array).
Every variable is aligned according to its own alignment requirements. Typically, a variable needs to be aligned on a boundary of its own size, i.e. a byte type must be byte-aligned, a word type word-aligned and so on. An array is always aligned according to its underlying type. Thus, to get a simple aligned type, just use a member of that type. To get an unaligned type, you need to allocate storage that is byte-aligned, and the easiest way of doing this is using a char[sizeof(T)]. I hope that answers the question as well.
Yes, that's correct. One minor note, though, is that the unaligned storage is actually char[num_bytes]. That's because sizeof(T) == num_bytes if the cover type is the same size as the endian type, but for, say, a 7 byte endian number the cover type is typically 8 bytes, so char[sizeof(T)] would be one byte too large. --Beman