C standard addressing simplification inconsistency
Posted
by
Chris Lutz
on Stack Overflow
See other posts from Stack Overflow
or by Chris Lutz
Published on 2011-02-05T06:34:51Z
Indexed on
2011/02/05
7:25 UTC
Read the original article
Hit count: 272
Section §6.5.3.2 "Address and indirection operators" ¶3 says (relevant section only):
The unary & operator returns the address of its operand. ... If the operand is the result of a unary
*
operator, neither that operator nor the&
operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue. Similarly, if the operand is the result of a[]
operator, neither the&
operator nor the unary*
that is implied by the[]
is evaluated and the result is as if the&
operator were removed and the[]
operator were changed to a+
operator. ...
This means that this:
int *i = NULL;
printf("%p", (void *) (&*i) );
printf("%p", (void *) (&i[10]) );
Should be perfectly legal, printing the null pointer (probably 0) and the null pointer plus 10 (probably 10). The standard seems very clear that both of those cases are required to be optimized.
However, it doesn't seem to require the following to be optimized:
struct { int a; short b; } *s = 0;
printf("%p", (void *) (&s->b) );
This seems awfully inconsistent. I can see no reason that the above code shouldn't print the null pointer plus sizeof(int)
(possibly 4).
Simplifying a &->
expression is going to be the same conceptually (IMHO) as &[]
, a simple address-plus-offset. It's even an offset that's going to be determinable at compile time, rather than potentially runtime with the []
operator.
Is there anything in the rationale about why this is so seemingly inconsistent?
© Stack Overflow or respective owner