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

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • You may find [this question](https://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) helpful for understanding what pointers are. This scenario is covered under the heading "memory leak". – Brian61354270 Dec 10 '20 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`. – user4581301 Dec 10 '20 at 20:54
  • @Brian: Thank you for the link. That explanation is a very good analogy of memory managment. It was helpful – Niranjan reddy Suravarapu Dec 10 '20 at 21:21

2 Answers2

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

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 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. – Niranjan reddy Suravarapu Dec 10 '20 at 21:43