what do you mean by automatic option? |
The std::unique_ptr. Check out what it does.
to check if the pointer is successfully deleted? |
You don't
delete the pointer. A pointer is a local variable, just like
int x;
The pointer's value is an
address. A number. A location.
The
delete
keyword first calls destructor of the object at the location and then deallocates the memory that is at that location. The deletion
does not change the value of the pointer. The pointer still has the same number, but now that address does not contain properly allocated memory.
You should do:
1 2
|
delete name;
name = nullptr;
|
We do not really care whether the deletion actually succeeds. If
delete name;
fails, what could you do?
Logically correct program would not attempt to access memory that it
assumes to be unavailable.
On the other hand, if the destructor does throw an exception, then we either handle it or the program exits.