Shao Miller said:
On 8/5/2013 14:39, Keith Thompson wrote: [...]
I think there's a DR (which I can't find at the moment) that says that
free(p) can actually change the representation of p -- which implies
that the unsigned char objects that make up its representation can have
their values change. If I'm remembering it correctly, I'm not at all
convinced that that can be derived from the normative wording of the
standard. (Can someone else find a reference to it?)
DR #260?:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_260.htm
Yeah, that's the one.
The committee response says, in effect, that the representation of a
pointer p can change after free(p):
Values may have any bit-pattern that validly represents them
and the implementation is free to move between alternate
representations (for example, it may normalize pointers,
floating-point representations etc.). In the case of an
indeterminate value all bit-patterns are valid representations
and the actual bit-pattern may change without direct action of
the program.
The DR mentions the issue that the bytes making up the
representation are themselves objects, and that the requirement
that "An object [...] retains its last-stored value throughout its
lifetime." implies that those bytes cannot change. The response
seems to ignore that argument.
On the other hand, I actually like the conclusion. As the DR
discusses, it permits certain optimizations
I think I would have preferred to have an explicit normative
statement added to the standard, saying that objects with
indeterminate values can have their representations change behind
the scenes.
Note that the response applies to more than just objects with
indeterminate values. If a type has multiple representations for the
same value, an object's representation can switch between those
representations -- which changes the values of the unsigned char objects
that make up its representation. This has implications for the use of
memcmp() to compare things other than byte arrays.