B
Ben Bacarisse
James Kuyper said:Chris said:Chris Torek wrote:
[The C Standard] *does* seem to say that:
(void *)&t == (void *)&t[0]
will always produce 1.
I would like to emphasise your answer in this way :
"It does *seem* to say that:"
So, if i understand well, the Standard doesn't make certain the
above equality yields 1 ?
I think that it *does* say that. The wording is not as clear as
I would like, though. If others disagree with me (about the result
of the comparison above always being 1), I would like to see their
reasoning, and perhaps a DR is called for.
When you convert a pointer from a pointer type to void*, where does
the resulting pointer point? I know where I and everyone else expects
it to point; I know where it will point under essentially every
implementation of C. I don't know where the standard actually says
so. There is such a statement for char*, but not for void*.
Hmm... I would argue that (void *)&obj must still be a pointer to the
object 'obj' so the wording of 6.3.2.3p7 applies to this pointer as
well. In other words (char *)(void *)&obj must point to the "first"
byte of obj. Since char * and void * must have the same
representation (the footnote suggesting interchangeability even in
unions) then (void *)&obj can't really be anything other a pointer to
the start of obj.
I would rather see a stronger statement to the effect that all
sequences of defined pointer conversions of a pointer value that end
with a value of type T * produce the same T * value[1]. I think would
not impact any real implementations but would ensure what most people
expect to be true. For example, given
T array[N];
we would know that (void *)&array == (void *)array and also that array
== (T *)&array. This is only one step further than the existing
wording that a converted pointer must convert back to the original
type. It extends it to converting back even after being further
converted (which might in fact be the simplest wording).
Of course if the existing wording (guaranteeing that a converted
pointer converts back to one that compares equal) is already intended
to apply even if the pointer is further converted then we are already
there.
[1] Strictly: that all such pointers compare equal.