Is it allowed to use a pointer's name of a pointer once deleted?
This code, for instance, won't compile.
int hundred = 100;
int * const finger = &hundred;
delete finger;
int * finger = new int; // error: conflicting declaration 'int* finger'
This won't either:
int hundred = 100;
int * const finger = &hundred;
delete finger;
int finger = 50; // error: conflicting declaration 'int finger'
No. The
int *
is still an living object. The lifetime of theint
that it pointed to has ended.Note that
has undefined behaviour, as you have attempted to
delete
an object that was not allocated bynew
.In general,
new
anddelete
should not appear in C++ programs. Owning pointers should bestd::unique_ptr
(or rarelystd::shared_ptr
or other user-defined smart pointer type).