Possible Duplicate:
Test for void pointer in C++ before deleting
Is code snippet 1 equivalent to snippet 2?
//Snippet 1:
delete record;
record = new Record;
//Snippet 2
if (record != NULL)
{
delete record;
record = NULL;
}
record = new Record;
The only difference I can see is that if the
Recordconstructor throws an exception, the first sample might leave therecordvariable set to the old deleted value, whereas in the second, it will be set toNULL.EDIT:
Indeed the first sample, if repeated later, would lead to a double delete.
The safe form would be:
Or use a
std::auto_ptrinstead of a raw pointer.