J
J de Boyne Pollard
s> int *f(int x)
s> {
s> return(&x); /* this address points to storage local of f() */
s> }
s>
s> such a returned pointer is ... invalid,
RP> False, not in agreement. The pointer is valid. A pointer
RP> can point to anything. The value from the derefenced
RP> pointer is invalid.
RP> [...] it can be returned and assigned to another pointer
RP> without being dereferenced...
Incorrect. There exist implementations (e.g. x86 architecture when
using far or huge data memory models and the AS/400) where
deallocation of storage causes the _values_ of any pointers to that
storage to become invalid. (Loading the value of the pointer into a
CPU pointer register causes a processor fault.) So simply using the
_values_ of the pointers, without even dereferencing them, causes
undefined behaviour. The C++ standard mentions this in regard to
storage with dynamic storage duration. The C standard is more general
and points out that the values of pointers to an object become
indeterminate after that object's lifetime, irrespective of storage
duration, ends.
s> {
s> return(&x); /* this address points to storage local of f() */
s> }
s>
s> such a returned pointer is ... invalid,
RP> False, not in agreement. The pointer is valid. A pointer
RP> can point to anything. The value from the derefenced
RP> pointer is invalid.
RP> [...] it can be returned and assigned to another pointer
RP> without being dereferenced...
Incorrect. There exist implementations (e.g. x86 architecture when
using far or huge data memory models and the AS/400) where
deallocation of storage causes the _values_ of any pointers to that
storage to become invalid. (Loading the value of the pointer into a
CPU pointer register causes a processor fault.) So simply using the
_values_ of the pointers, without even dereferencing them, causes
undefined behaviour. The C++ standard mentions this in regard to
storage with dynamic storage duration. The C standard is more general
and points out that the values of pointers to an object become
indeterminate after that object's lifetime, irrespective of storage
duration, ends.