4

Let say I have a hypothetical pointer declared with new like so:

int* hypothetical_pointer = new int;

and create another hypothetical pointer, with the same value:

int* another_hypothetical_pointer = hypothetical_pointer;

If I were to go about deleting these, which were declared with new, would I have to delete both pointers, or only the one explicitly declared with new? Or could I delete either pointer?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 5
    This sort of question is why some of us recommend smart pointers in C++. – David Thornley Dec 08 '10 at 21:30
  • 1
    I hope all of us recommend smart pointers. – Martin York Dec 08 '10 at 21:38
  • 1
    But it's the sort of question **everyone** face, necessarily. If you code in C++ you're going to face this issue. It's a good thing: it means the one thing you're going to thoroughly and properly understand is why we use smart pointers. **Fail early, fail often!** – wilhelmtell Dec 08 '10 at 21:44
  • @wilhelmtell "Fail early, fail often!" I like that. –  Dec 08 '10 at 21:46

4 Answers4

15

delete destroys the dynamically allocated object pointed to by the pointer. It doesn't matter if there are one or 100 pointers pointing to that object, you can only destroy an object once.

After you delete hypothetical_pointer;, you cannot use hypothetical_pointer or another_hypothetical_pointer because neither of them points to a valid object.

If you need to have multiple pointers pointing to the same object, you should use a shared-ownership smart pointer, like shared_ptr, to ensure that the object is not destroyed prematurely and that it is destroyed exactly once. Manual resource management in C++ is fraught with peril and should be avoided.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • @wilhelmtell: I'll see your RAII and raise you one SBRM. – James McNellis Dec 08 '10 at 21:44
  • 1
    For those who are curious, Resource Acquisition Is Initialization (RAII) and Scope-Bound Resource Management (SBRM) are the names of the general design pattern used to implement smart pointers. It is the most important design pattern to understand and to utilize when programming C++. – James McNellis Dec 08 '10 at 21:51
8

There is only one block of memory from the single call to new, so you must delete it once and only once, when all users are done with it.

boost::shared_ptr is a nice way to accommodate multiple users of the same data - the last reference going out of scope triggers delete on the underlying memory block.

boost::shared_ptr<int> ptr1(new int);
boost::shared_ptr<int> ptr2(ptr1);

// memory deleted when last of ptr1/ptr2 goes out of scope
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
4

Only call delete on one of those pointers - doesn't matter which, since they both contain the same value, as a pointer. Do not use either pointer after calling delete.

Thanatos
  • 42,585
  • 14
  • 91
  • 146
  • That is correct, but not useful. Try to do that without smart pointers and you'll see why it's difficult. You might have two pointers completely independent of each other and opaque to each other; when one frees the resource the other knows nothing of it. – wilhelmtell Dec 08 '10 at 22:04
  • The question was not about smart pointers. As I commented in another answer, they can be useful in circumstances like this, but that is an assumption you are making about the OP's situation. There are times when a smart pointer is not justified, and you need to use pointers, and understanding the semantics involved is necessary to do so. Copying a pointer into another instance is not that uncommon: the exact circumstances at play were not mentioned by the OP. – Thanatos Dec 08 '10 at 22:48
2

Correct answer is that you don't delete either. You delete what they point at.

Now...if they both point at the same thing, how many times do you think you should delete what they point at?

Edward Strange
  • 40,307
  • 7
  • 73
  • 125