On 22 Jan, 09:14, James Kanze <
[email protected]> wrote:
[...]
Further drawback: You dont have any size information of the
bytes array. Slightly better would be:
union
{
long l;
bytes b[4];
} u;
That's formally worse, since it results in undefined
behavior if you access b when the last value was written to
l.
no. If bytes is an alias for unsigned char the it merely
results in implementation defined behaviour. unsigned char
is the exception to the usual "only read what you wrote to a
union".
Where does it say that? All I see is that "only one of the
members can be active at any time". Formally, I think that this
still remains undefined. Taking the address of l, casting it
to an unsigned char*, and accessing through that is legal, but
rather defeats the purpose of using a union.
In practice, of course, it will depend on the compiler. (Also,
I don't think the results are even "implementation defined".
More along the lines of "unspecified", or "very poorly
specified". But normally: "what the hardware gives you", which
may or may not be what is wanted.)
You can access any object as a array of unsigned char.
[caveat: this is true for C at least, I can't see why C++
would change it]
C++ even extended it to encompass accessing it as an array of
char (which more or less means that plain char must be unsigned
if the machine is not 2's complement). But even something like:
union
{
char c1 ;
char c2 ;
} u ;
u.c1 = 'a' ;
putchar( u.c2 ) ;
is undefined behavior---the standard allows the implementation
to somehow maintain the information as to what the last stored
value was (except in some very special cases), and core dump if
you access via any other member.
(That was, at least, the concensus in the C committee, back in
the late 1980's. At least among some of the committee members.)