0

In C++, whats the recommended way of deleting a pointer? For example, in the following case, do I need all three lines to delete the pointer safely (and if so, what do they do)?

// Create
MyClass* myObject;
myObject = new MyClass(myVarA, myVarB, myVarC);

// Use
// ...

// Delete
if(myObject) { 
    delete myObject; 
    myObject = NULL; 
}
Alex
  • 9,313
  • 1
  • 39
  • 44
Ben
  • 15,938
  • 19
  • 92
  • 138

4 Answers4

6

No you do not need to check for NULL.
delete takes care if the pointer being passed is NULL.

delete myObject; 
myObject = NULL; 

is sufficient.

As a general policy, avoid using freestore allocations wherever you can, and if you must use Smart Pointers(RAII) instead of raw pointers.


C++03 Standard Section §3.7.3.2.3:

The value of the first argument supplied to one of the deallocation functions provided in the standard library may be a null pointer value; if so, the call to the deallocation function has no effect. Otherwise, the value supplied to operator delete(void*) in the standard library shall be one of the values returned by a previous invocation of either operator new(size_t) or operator new(size_t, const std::nothrow_t&) in the standard library, and the value supplied to operator delete in the standard library shall be one of the values returned by a previous invocation of either operator new or operator new[](size_t, const std::nothrow_t&) in the standard library.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    @R.MartinhoFernandes: Practically, I see no real overhead in writing an extra line of code. While, It's a touchy topic & purists seldom debate as to whether one actually needs to do that, I prefer doing it since doing so doesn't yield me any ill effects but gives me an possible safety assurance. – Alok Save Oct 03 '11 at 04:13
4

No, you don't need that.

Just write delete p, and be sure to not accept advice about anything else from anyone advocating that you do more.

General advice: avoid raw new and delete. Let some standard container deal with allocation, copying and deallocation. Or use a smart-pointer.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
3

Best to use a resource-managing class and let the library take care of that for you:

#include <memory>


void foo()  // unique pointer
{
  std::unique_ptr<MyClass> myObject(new Myclass(myVarA, myVarB, myVarC));
  // ...
}  // all clean

void goo()  // shared pointer -- feel free to pass it around
{
  auto myObject = std::make_shared<MyClass>(myVarA, myVarB, myVarC);
  // ...
}  // all clean

void hoo()  // manual -- you better know what you're doing!
{
  MyClass * myObject = new MyClass(myVarA, myVarB, myVarC);
  // ...
  delete myObject;  // hope there wasn't an exception!
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Could you explain the differences between these pointers, please? – Ben Nov 24 '11 at 11:47
  • See [this question](http://stackoverflow.com/questions/6876751/differences-between-unique-ptr-and-shared-ptr) and links therein. – Kerrek SB Nov 24 '11 at 13:06
2

Just to add that setting the pointer to NULL after it's been delete'd is a good idea so you don't leave any dangling pointers around since attempts to dereference a NULL pointer is likely to crash your application straight away (and so be relatively easy to debug), whereas dereferencing a dangling pointer will most likely crash your application at some random point and be much more difficult to track down and fix.

lee-m
  • 2,269
  • 17
  • 29
  • 2
    it's never been a good idea: it just hides bugs and causes more work – Cheers and hth. - Alf Oct 02 '11 at 14:42
  • It is always a good idea to set the pointer to null after deleting it *if* you are doing your own memory management (i.e., using `new` and `delete`). Even when you *know* that nobody will access that pointer (e.g., the `delete` is in a destructor), it is still a good idea, and the cost is minimal. That it costs more work (one small little line of extra work) is just one of the many costs of managing memory yourself. – David Hammen Oct 02 '11 at 16:02