This code has an undefined behavior, as there is an attempt to double-delete the memory addressed by the pointer a , and the wrong delete statement is used a second time.
int *a = new int[10]; delete [] a; delete a; a = nullptr;
It is usually advised to reset the pointer after releasing the memory indicated by it, so that when you try to delete the memory again, you will not get a program execution error. For example, this code is correct.
int *a = new int[10]; //... delete [] a; a = nullptr; //... delete [] a;
since you can call the operators delete and delete[] for null pointers.
However, by doing so you mask a potential error in your program. It is possible that your program is malfunctioning and, indeed, is trying to at least twice delete the same area of memory. If you assign a nullptr value to the pointer, then it will be difficult for you to detect this error.
The argument for assigning a value to nullptr is that if you have a pointer equal to nullptr , then it will be easy for you to detect an attempt to access memory using that pointer. However, in reality this is not always the case. For example, the z/OS operating system on IBM mainframe allows you to read memory using a null pointer. It only does not allow to write data on such a pointer. Therefore, again, by assigning a nullptr value nullptr pointer, you can disguise the error when you access the readable memory at the zero address. In my experience, such a mistake is often made by C programmers working on the IBM mainfarme when using standard string functions from the <string.h> library, calling, for example, the string comparison function strcmp , when one of the pointers is NULL . As a result, the program has an undefined behavior. But to detect such an error is very difficult.
On the other hand, explicitly assigning a nullptr value to the nullptr facilitates self-documenting of the source code. The reader is given to know that, starting with this assignment, the pointer is invalid.
So there is no universal rule. It is advisable to make it so that the lifetime of the variable designating the pointer matches the lifetime of the memory it points to. In this case, there is no point in assigning a nullptr value to the nullptr , since the pointer ceases to exist at the same time as the memory addressed by it is deleted.
Most often, you can come across a code where either initialization of the pointer by zero is used, or assignment to it null values in old C programs, since previously, before adopting the C99 standard, variables could be declared only at the beginning of the code blocks. Therefore, between the declaration of the pointer and its use or between the removal of the memory addressed by the pointer and the termination of the pointer itself, there are often many lines of code that are difficult to cover with one look at the source text. Therefore, assigning null values to pointers allowed the code reader to more easily understand the logic of this or that cumbersome function.
Currently, both in C and in C ++, you can declare variables anywhere in a code block, and therefore you can narrow the lifetime of variables to the part of the code where they are directly used. Therefore, there is no longer any need to assign a null value to the "spent" pointer.
Try to declare variables in the smallest declaration area where they are directly used. so that there are no "pending" variables that cannot be said about whether they are used elsewhere in the code, or whether they are no longer used.
In addition, in C ++ there are so-called smart pointers, as well as corresponding containers that themselves take care of deleting the memory that is no longer in use. The allocation and deletion of memory "manually" is fraught with errors in the program.