In article <11**********************@a3g2000cwd.googlegroups. com>,
<p_***********@yahoo.co.inwrote:
unsigned char buff[20];
unsigned int i;
i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */
Yes, because you don't know that buff has been properly aligned
to write an int into the beginning of it.
Also, you are assuming that unsigned int is big enough to hold
something 32 bits wide; an unsigned int need only be 16 bits wide to
satisify the value constraints.
Your constant is written as an unsigned long and that is assigned
into the unsigned int. That's odd enough to stand out, but it should
not be a problem in practice: if unsigned long is wider than
unsigned int, the unsigned long will not have any sign extension,
and even if it did, that sign extension would effectively be removed
when assigned into the unsigned int.
Your assignment involves type punning on a plain int rather than
an unsigned int. There is the possibility that an arbitrary
unsigned int might become a trap representation when the same
bit pattern is used as an int; such problems become more -likely-
when the unsigned int has bits set that correspond to sign bits
in the int. The particular value you chose, 0xaabbccdd, is -likely-
to overlay the top bit of 0xaa or 0xcc onto the sign bit, so you -could-
be getting into trouble that way. Safer to convert through
unsigned int * .
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest