1

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.

6 Answers 6

5

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...)

Sign up to request clarification or add additional context in comments.

Comments

5

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.

1 Comment

Or in a double free, which is even worse.
1

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.

2 Comments

It's important to note that he would have to set myPointerToMyA to NULL, not myA (since you didn't make that entirely clear).
Setting the pointer to NULL will not do anything for the delete on the second pointer.
1

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.

Comments

0

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.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.