-1
 Class example { }; int main() { Example* pointer1 = new example(); Example* pointer2; pointer2 = pointer1; delete pointer1; } 

Should I delete pointer2? I think it's in the stack and I don't need to delete.

8
  • 3
    No, the memory it is pointing to is already freed by deleting pointer1 Commented Oct 29, 2019 at 13:24
  • Might not hurt to assign it to null_ptr though as it will be a hanging pointer if anything else is done after the delete. Commented Oct 29, 2019 at 13:25
  • If you new it, you should delete it. Once. If you pass along ownership, you are passing along the responsibility to delete it, and the passer is relinquishing that responsibility. (But it is better to express that ownership more explicitly in the code by using std::unique_ptr or std::shared_ptr.) Commented Oct 29, 2019 at 13:27
  • 1
    No, pointer2 will be a dangling pointer. You need to set it to nullptr yourself (if that is important to your code). Commented Oct 29, 2019 at 13:27
  • 1
    You do not delete pointers - you delete objects by using pointers. Commented Oct 29, 2019 at 13:41

2 Answers 2

1

Short answer: No.

pointer1 and pointer2 are both pointers which exist on the stack. new Example allocates on the heap a new object of type Example, and its memory address is stored in pointer1. When you do delete pointer1, you are freeing the memory allocated on the heap. Since both pointer1 and pointer2 are both referencing the same memory location at the point of the delete call, it does not need to also be deleted, in fact, this would be Undefined Behaviour, and could cause heap corruption or simply your program to crash.

At the end of this, both pointer1 and pointer2 are still pointing to the same block of memory, neither are actually nullptr.

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

1 Comment

This is a wordy way of saying that every call to new must be matched by exactly one call to delete. Also careful on the terms heap and stack those terms, these are not strictly part of C++; you should use the terms automatic storage duration and dynamic storage duration. The reason I bring it up as you get into the more complex details heap and stack become confusing while storage duration continues to work. I suppose in this situation it works but be careful.
0

Deleting a pointer is telling the operating system that the memory at the location of that pointer is not needed anymore by the program. Remember that a pointer is just an integer that points to place in your RAM. By doing pointer2 = pointer1, you're only copying the integer and you're not moving any memory around. Therefore, by deleting the first pointer, because the second pointer points to the same location, you don't need to delete it.

3 Comments

Everything that is held in memory is an "integer", I would rather refer to things a tiny bit more carefully. In your example I would suggest you replace "integer" with "address".
@MartinYork I am aware of that but an address is just an integer that points to a place in memory and that's easier for a beginner to understand. Sorry if I didn't use the best term for describing a pointer but i think it's more important for Kaepxer to understand then use the most correct term.
You are actually making a whole bunch of assumptions in that single term. But its your answer. In my opinion it does more harm than good so -1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.