views:

124

answers:

6

Say we have a piece of code:

//...
class A
//...
A* myA = new A();
A* myPointerToMyA = myA;
delete myA;
delete myPointerToMyA; // this is wrong, no?
//...

The last line does the exact same thing as the one above it, correct? So I would now be deleteing an invalid/NULL pointer?

I understand this may be a stupid question, but still, I need some reassurance.

A: 

Yes it is wrong. The memory allocated for allocated with new A() and released with delete myA. One thing to note is that while delete myPointerToMyA is an attempt to delete an invalid pointer, it isn't an attempt to delete a NULL pointer because myPointerToMyA is not equal to NULL.

torak
+1  A: 

You are correct.

So I would now be deleteing an invalid/NULL pointer?

Well, technically it's only invalid, because nothing was set to NULL. It's ok to delete a NULL pointer.

Cogwheel - Matthew Orlando
It's important to note that he would have to set myPointerToMyA to NULL, not myA (since you didn't make that entirely clear).
Niki Yoshiuchi
Setting the pointer to NULL will not do anything for the delete on the second pointer.
Noah Roberts
Clarified. Thanks.
Cogwheel - Matthew Orlando
+1  A: 

What you are getting here is the following:

A* myA = new A();          // myA is now equal to 0x11110000 for example(!)
A* myPointerToMyA = myA;   // myPointerToMyA is now equal to 0x11110000
delete myA;                // equal to delete (A*)(0x11110000)
delete myPointerToMyA;     // equal to delete (A*)(0x11110000)

Two last lines are equal in the end. This code will lead to undefined behavior.

Kirill V. Lyadvinsky
+5  A: 

Indeed it is wrong. And in constrast to one of the other comments, it's not because you didn't allocate it with new.

Both myA and myPointerToMyA point at the same thing. Deleting through either of them is fine - but you can only delete it once legitimately because they point at the same thing - it is what is pointed at that is deleted, not the pointer itself.

There is nothing wrong with having two pointers to the same thing, but you musy keep track of who owns it, and who is responsible for deleting it.

In this instance, deleting a pointer to a deleted object, the behavior is 'undefined' - the run-time can do what it likes! (I can pretty much guarantee you won't like it...)

Ragster
+4  A: 

Yes, it's wrong. When you used delete, you're not deleting the pointer. Rather, you're deleting what it points to. So, when you use delete on a pointer, the memory that that pointer points to is freed. Any other pointer that points to that memory is now pointing to unallocated memory and is a dangling pointer. Using a dangling pointer results in undefined behavior, and it certainly isn't valid to try and free already freed memory, so using delete on a dangling pointer is definitely wrong. It will likely result in a segmentation fault.

Jonathan M Davis
Or in a double free, which is even worse.
Matteo Italia
A: 

2 pointers point to the same object. The object gets destroyed after you call delete myA; for the first time. When you call delete for the second time(delete myPointerToMyA;) you are trying to delete object more than once, and the result of such action is undefined(usually you get runtime exception).

a1ex07