N1570 states that this is undefined behavior:
§J.2/1 The value of an object with automatic storage duration is used while it is indeterminate (6.2.4
I only have access to N1256 but I will be surprised if there is any material change.
The most relevant section is "6.5.3.2 Address and indirection operators"
And in particular paragraph 3 (my emphasis):
Semantics
3 The unary & operator yields the address of its operand. If the operand has type ''type'', the result has type ''pointer to type''. 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. Otherwise, the result is a pointer to the object or function designated by its operand.
None of the cited paragraphs in the OP apply because as many have pointed out. The value of something and its address are very different.
I would argue that the absence of any restriction on & regarding uninitialized values taking their address is permitted (by the absence of prohibition).
NOTE: We all know it's fine but it's very rare to find statements in these standards that explicitly say "Yes you can do so & so."
In this expression:
&ptr
the address of the &
operand, i. e., the address of the ptr
object is yielded but the ptr
object is never evaluated.
(C11, 6.3.2.1p2) "Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue); this is called lvalue conversion."
No. It's not undefined behaviour. Only ptr
is uninitialized and has indeterminate value but &ptr
has a proper value.
What the standard quote on strtol
says:
...If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer.
That above quote is talks about a call like this:
strtol(str, 0, 10);
The call in the man page and the linked answer are perfectly fine.
The pointer's value and its address are not the same.
void *foo;
That pointer has an undefined value, but the address of foo
, i.e. the value of &foo
, must be well-determined (since otherwise we can't access it).
At least that's my intuitive understanding, I didn't dig up the standard now, I just think you're mis-reading it.
When talking about code, the two are sometimes confused ("what's the address of that pointer?" can mean "what's the value of that pointer, what address is it pointing to?") but they are really distinct.
See this example
char * ptr;
Since ptr
is not pointing to any object, dereferencing it invokes undefined behavior. But when you pass its address to strtol
, having syntax
long int strtol(const char *nptr, char **endptr, int base);
in statement
long parsed = strtol("11110111", &ptr, 2);
the endptr
parameter of strtol
is pointing to object ptr
and derefeencing it will not invoke any UB.