Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-08-11 08:34:11


christopher diggins wrote:

> 3.9(2) says:
> For any complete POD object type T, whether or not the object holds a valid
> value of type T, the underlying
> bytes (1.7) making up the object can be copied into an array of char or
> unsigned char.36) If the content of the array of char or unsigned char is
> copied back into the object, the object shall subsequently hold its original
> value.
>
> This has nothing to do with void* and char*, but rather with all POD's.
>
> This clearly implies char arrays are stringently aligned.

Not to me, it doesn't! It doesn't say "can be copied into *any* array
of char". And it doesn't say you can use reinterpret_cast to use the
char array as an object of T, it says you can store the bytes there then
copy them back into a T, without loss of information.

You are implying this is OK:

#include <cstring>

typedef int T;
struct S {
    char a1[3];
    char a2[sizeof(T)];
};
int main()
{
    T t;
    S s;
    std::memcpy(s.a2, &t, sizeof(T));
    T* p = reinterpret_cast<T*>(s.a2);
    return *p;
}

That's nonsense. S.a2 is not correctly aligned for an int.

jon


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk