1
#include <iostream>
using namespace std;
int main() {
    int *p1;
    p1 = new int;
    int *p2;
    p2 = new int;
    p2 = p1;    // what happens here?
    *p1=5;
    cout << "pointer 2 is " << *p2 << endl << *p1 << endl;  // both give out 5
    delete p1;  // what happens to p2 ?
    cout << "pointer 2 is " << *p2 << endl;
    delete p2;
    return 0;
}

What happens when I delete the pointer object p1? What is pointer p2 referencing now ? Can some one explain ? Thanks for the help

3
  • You may find this question helpful for understanding what pointers are. This scenario is covered under the heading "memory leak". Commented Dec 10, 2020 at 20:53
  • Important point: A pointer is a variable that happens to contain an address and can be used to access the object at that address. Other than that it's just like any other variable, so p2 = p1; replaces the address stored by p2 with the address stored by p1. Commented Dec 10, 2020 at 20:54
  • @Brian: Thank you for the link. That explanation is a very good analogy of memory managment. It was helpful Commented Dec 10, 2020 at 21:21

2 Answers 2

6

What is pointer p2 referencing now ?

Nothing. It's dangling. It was pointing to the same thing p1 was pointing to, but you've deleted that.

Consequently, your *p2 is broken, as is your delete p2; these both have undefined behaviour.

You've also leaked the second new int, because p2 used to point to it but stopped doing so when you wrote p2 = p1 (you changed it to point to the first new int instead, like p1 does), and there's no other way to refer to it.

Crude diagram of what happens in the OP's code, with respect to pointers

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

3 Comments

Nice diagram. I would suggest including the last cout << *p2 statement in the fire, too
@RemyLebeau Good idea
@Asteroids With Wings my god! that was wonderful. Apparently my code has two sins.. memory leak, trying to delete a dangling object. I liked the fire part of it
4
p2 = p1;    // what happens here?

You are simply copying the value (pointed memory address) of p1 into p2. This is no different than doing this:

int i1, i2;
i1 = 12345;
i2 = i1;

However, because the value in question in a memory address allocated with new, you now have a memory leak, as you have lost your only pointer to the 2nd allocated int that p2 was previously pointing at. Now p1 and p2 are both pointing at the 1st int in memory.

cout << "pointer 2 is " << *p2 << endl << *p1 << endl;  // both give out 5

Yes, because p1 and p2 are pointing at the same memory address.

delete p1;  // what happens to p2 ?

Nothing happens to p2 here, it is still pointing at the same memory address it was before. p1 and p2 are independent variables, so a change to one does not affect the other. But, the delete has deallocated the 1st int that was stored at the memory address being pointed at by p1 and p2, so now they are both dangling pointers to invalid memory. Since you don't use p1 after this, that is ok for p1, but for p2 the next statements:

cout << "pointer 2 is " << *p2 << endl;
delete p2;

Will cause Undefined Behavior, because you are accessing invalid memory via the dangling p2. The read may succeed in returning stale data that is still present at the memory address, or it may return garbage, or it may crash. The delete will almost certainly crash, since the memory has already been freed earlier, but that is not guaranteed either. Undefined Behaivor is just that - undefined - so literally anything could happen.

1 Comment

Thank you the detailed explanation of the what went wrong especially trying to access object that p2 is pointing to. This was the error message I got while executing the program : "free(): double free detected in tcache 2 Aborted". I understand now what happended.

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.