Assume a dangling reference x
. Is it undefined behavior to just write
&x;
or even
x;
?
See Question&Answers more detail:osAssume a dangling reference x
. Is it undefined behavior to just write
&x;
or even
x;
?
See Question&Answers more detail:osFirst off, very interesting question.
I would say it is undefined behaviour, assuming "dangling reference" means "referred-to object's lifetime has ended and the storage the object occupied has been reused or released." I base my reasoning on the following standard rulings:
3.8 §3:
The properties ascribed to objects throughout this International Standard apply for a given object only during its lifetime. [ Note: In particular, before the lifetime of an object starts and after its lifetime ends there are significant restrictions on the use of the object, as described below ...]
All the cases "as described below" refer to
Before the lifetime of an object has started but after the storage which the object will occupy has been allocated38 or, after the lifetime of an object has ended and before the storage which the object occupied is reused or released
1.3.24: undefined behavior
behavior for which this International Standard imposes no requirements [ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. ...]
I apply the following train of thoughts to the above quotes: