Consider:
int* ptr = (int*)0xDEADBEEF;
cout << (void*)&*ptr;
How illegal is the *
, given that it's used in conjunction with an immediate &
and given that there are no overloaded op&
/op*
in play?
(This has particular ramifications for addressing a past-the-end array element &myArray[n]
, an expression which is explicitly equivalent to &*(myArray+n)
. This Q&A addresses the wider case but I don't feel that it ever really satisfied the above question.)
&*ptr
toptr
; so practically ok. But theoretically it can be an undefined behavior. – iammilind*ptr
is not an object), but no lvalue-to-rvalue conversion takes place on the non-existent referand (so optimistically: it'll work). – Steve Jessop&myArray[n]
for an array ofn
elements is illegal, and you should instead be doing&myArray[0] + n
. In other words, it is OK to walk a pointer beyond the end of an array, but it is illegal to dereference the pointer once it is beyond the bounds of the array. – Praetorian&*E
is equivalent toE
; this is explicitly allowed. So the only problem that remains (in C) is whether creating an invalid pointer in the first place is undefined. I've never found the equivalent language in the C++ standards. – Oliver Charlesworth