2 pointers pointing to same variable

say, we have 2 pointers pointing to same variable
if i delete the one, what would happen if i dereferenced the other one?
You don't delete pointers, you delete the memory the pointer points to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int* a = new int;  // new creates an unnamed integer.  Let's call it 'foo'
   // 'a' points to 'foo'

int* b = a;  // 'b' also points to 'foo'

// at this point, both 'a' and 'b' are pointing to the same thing:  'foo'

delete a;  // does not delete a, but instead deletes what a point to.
   // since 'a' points to 'foo', this deletes 'foo'
   // 'foo' no longer exists


// a and b did not change.  They still point to the memory once assigned to 'foo', but since
// 'foo' no longer exists, they are BAD POINTERS and attempting to dereference either
// of them results in undefined behavior 
The same thing if you dereference deleted one. Undefined behavior. You are trying to access memory which is no longer belongs to the program.
got it guys! thanks, this is a late reply sorry
Topic archived. No new replies allowed.