Richard said:
Or that it has undefined behaviour. Integers with a size that is
relatively prime to 0x200 are scarce to the point of non-existence and
AFAIAA upto and including that point, but they are legal; and therefore,
so are integers for which 0x200 is converted into a badly aligned
pointer. (Come to think of it, I don't see that requiring ints to be
aligned to odd addresses is illegal. So even with the most commonly
expected conversion of 0x200 to int pointer type and with 2-byte ints,
(int *)0x200 could in theory be a non-aligned pointer.)
I'd written about this in my original reply on the thread,
but cut that part before sending on the grounds that it was an
unnecessary, hair-splitting, angel-counting departure from the
real question at hand. "Let it pass," thought I, "The O.P. is
asking about embedded environments where special needs abound
and this sort of dubious conversion is the only way to get things
done. Why drag in irrelevancies?"
You'd think I'd know better. I've been following c.l.c. for
twenty years, and should have learned that while irrelevancy may
not be the meat and potatoes of its diet, it is at least the
eggplant.
In the interest of balanced nutrition, then:
1) Converting an integer to a pointer produces a result
that is implementation-defined. No undefined behavior!
2) The implementation-defined result need not be valid.
It may be improperly aligned, it may "point to nowhere," it
may even be a trap representation. (In which case, the result
is not even a "value," properly speaking.)
3) If the result is a trap representation or is incorrectly
aligned, the act of assigning it to a pointer causes undefined
behavior.
4) If the result is an invalid value, the attempt to use
it to reference an object causes undefined behavior.
N) THEREFORE: The O.P.'s fear that the conversion is undefined
are groundless; it's implementation-defined. The possibility of
U.B. doesn't arise until *after* the conversion. What a relief!
"Don't worry, King Louis, nobody's going to shoot you."