4

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;
Community
  • 1
  • 1
blitzkriegz
  • 9,258
  • 18
  • 60
  • 71
  • 1
    duplicates (only the 1st page of results, there are many more): http://stackoverflow.com/questions/615355/is-there-any-reason-to-check-for-a-null-pointer-before-deleting http://stackoverflow.com/questions/4190703/is-it-safe-to-delete-a-null-pointer http://stackoverflow.com/questions/3821261/null-check-before-deleting-an-object http://stackoverflow.com/questions/724688/how-many-of-you-are-aware-that-its-safe-to-delete-a-null-pointer http://stackoverflow.com/questions/1558013/delete-null-but-no-compile-error http://stackoverflow.com/questions/3844374/test-for-void-pointer-in-c-before-deleting – Alexandre C. Jan 26 '11 at 12:02

6 Answers6

8

The only difference I can see is that if the Record constructor throws an exception, the first sample might leave the record variable set to the old deleted value, whereas in the second, it will be set to NULL.

EDIT:
Indeed the first sample, if repeated later, would lead to a double delete.

delete record;
record = new Record; // Throwing exception
// record points to old object
// ... later ...
delete record; // Double deletion - sky falls down etc.

The safe form would be:

delete record; record = 0;
record = new Record; // Can throw an exception if it likes

Or use a std::auto_ptr instead of a raw pointer.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • ... which might lead to a double-delete issue, as James pointed out (just adding for completeness). – etarion Jan 26 '11 at 12:12
6

It depends on the scope of record. If it's a class member and is deleted in the destructor then no, they're not the same. The first version can leave you with a double-delete issue (if new Record() throws) because record is not NULL.

If it has function-level scope then I think the second version is overkill.

The second version is more safe.

James
  • 9,064
  • 3
  • 31
  • 49
  • good catch. You might want to mention that this is in the case where the Record constructor throws and it's about assigning 0 to the pointer, not about the check for null before deleting. – etarion Jan 26 '11 at 12:11
  • +1 for spotting what the questioner and all the close-voters missed: the title doesn't reflect the body of the question. – Steve Jessop Jan 26 '11 at 13:46
3

They are the same in that you have a new Record at the end of each, but snippet two has an unnecessary check for NULL prior to the delete.

Skizz
  • 69,698
  • 10
  • 71
  • 108
1

The delete operator in C++ is required to work correctly (do nothing) when the pointer is NULL, so the two code fragments are the same. See http://opensource.devx.com/tips/Tip/14443, for example.

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
1

Yes, it is: delete on a NULL pointer is expected to work as in your latter code.

See $ 5.3.5:

2 If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In either alternative, the value of the operand of delete may be a null pointer value.

Simone
  • 11,655
  • 1
  • 30
  • 43
1

A nitpicking answer: The result is the same IF record is a pointer to record (with the caveats described in the other posts wrt. the record constructor throwing). If it's a class type with an implicit conversion to a pointer, the result might not be the same (because operators might be overloaded.)

etarion
  • 16,935
  • 4
  • 43
  • 66