Also, it's never REQUIRED to set anything back to NULL, but it's useful for debugging purposes. As a general rule, if the pointer doesn't point to anything, make it NULL. For example, this is legal:
Code:
int * piVar;
/* == More code (1) == */
piVar = new int[30];
/* == More code (2) == */
delete[] piVar;
/* == More code (3) == */
but here, piVar is only useful during the block labelled "More code (2). If it was used at some point during "More code (1)" or "More code (3)", it MIGHT give us an error, or (even worse) it might only fail during unpredictable cases. That is particularly likely in the case of More code (3). Code that appears to work is the worst kind of broken code. I made that bold because I cannot stress enough how much trouble "working" broken code can cause.
Look at this better code below:
Code:
int * piVar(NULL);
/* == More code (1) == */
piVar = new int[30];
/* == More code (2) == */
delete[] piVar;
piVar = NULL;
/* == More code (3) == */
piVar is still only usable during block #2, but now any attempt to use it during blocks #1 or #3 will fail at runtime, all the time. They would still be errors, but they couldn't hide behind the mask of unpredictable success, so we can isolate and eliminate them with relative ease. Further, if we always keep all pointers NULL unless they point to something, then we can simply test to see if a pointer is NULL to know if it points to anything or not.
Lastly it often makes destructors easy. It's legal to call delete or delete[] on a NULL pointer (the standard says it will do nothing, and is not an error), so we can delete all pointers without checking if they are valid. If they are valid, they are deleted. If they weren't valid, they should be NULL and "deleting" them does no harm.