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 the int
that it pointed to has ended.
Note that
int hundred = 100;
int * const finger = &hundred;
delete finger;
has undefined behaviour, as you have attempted to delete
an object that was not allocated by new
.
In general, new
and delete
should not appear in C++ programs. Owning pointers should be std::unique_ptr
(or rarely std::shared_ptr
or other user-defined smart pointer type).