-1

Here is an image with program working:

image

Why, when I delete one of the pointers that point at a memory location, I can change the value in another one without error? Why does the address of the deleted pointer change? I read on the Internet that the address shouldn't change after a delete. Does it depend on the compiler?

int* ptr = new int(7);
int* _ptr = ptr;
cout << ptr << '\n'; //print - 00CE4ED0
delete ptr;
cout << ptr << '\n'; //print - 00008123
cout << *_ptr << '\n'; //in this moment cout<<*_ptr; give garbage insdead of error . WHY?!
*_ptr = 10;
cout << *_ptr << '\n'; //print 10
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Yura Pazyn
  • 11
  • 3
  • 8
    `delete` doesn't destroy a pointer. It destroys the object the pointer points to. After `delete ptr;` both `ptr` and `_ptr` point to a destroyed object which you aren't allowed to use anymore. – François Andrieux Sep 15 '21 at 15:20
  • 1
    how did you conclude that the adress changed? You are only dereferencing the pointer, you arent looking at the valeu of the pointer itself – 463035818_is_not_an_ai Sep 15 '21 at 15:23
  • I think you mean 'value' instead of address. Accessing the value of a pointer after deleting the data to which it points is 'undefined behavior', and should be avoided at all costs. Consider using std::unique_pointer or std::shared_pointer (assuming you're not in university learning C++) – optimus_prime Sep 15 '21 at 15:24
  • Francois has it right. Once *ptr (which is the same as *_ptr) is destroyed by delete ptr, it must no longer be accessed. Other point: as ptr is an array, you meant delete [] ptr not delete ptr. – Topological Sort Sep 15 '21 at 15:27
  • Does this answer your question? [Deleting a heap then dereferencing a pointer to that memory](https://stackoverflow.com/questions/927945/deleting-a-heap-then-dereferencing-a-pointer-to-that-memory) – Mgetz Sep 15 '21 at 15:35
  • Thanks to everyone for your help! ) – Yura Pazyn Sep 15 '21 at 19:13

1 Answers1

1

I can change value in another one without error?

You have Undefined Behavior, because you are dereferencing a pointer to a deleted object here.

cout<<*_ptr; 

C++ does not promise you an error when you perform Undefined Behavior. Instead, anything may happen.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Thanks, I undersdant this but I have no clue about the reason why after delete ptr I can do this(*_ptr=10) and this works?! I obviously can't do (*ptr=10). Please see screenshot that I add in description. – Yura Pazyn Sep 15 '21 at 17:48
  • 1
    @YuraPazyn how did you decide that you "obviously can't do (*ptr=10)"? [You can](https://godbolt.org/z/nv9MGK837). And again, it's called [Undefined Behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – Drew Dormann Sep 15 '21 at 17:53
  • Do I understand correcly Undefined Behavior handling can be different and depends on compiler, some compilers give an error other no? – Yura Pazyn Sep 15 '21 at 18:04
  • @YuraPazyn in general, Undefined Behavior is not "handled". That's why it's _undefined_. – Drew Dormann Sep 15 '21 at 18:39
  • Yes, Yura, that's the right understanding. It does depend on the compiler (at least). So it is best avoided. – Topological Sort Sep 15 '21 at 18:53
  • Topological Sort, Drew Dormann Thank you! – Yura Pazyn Sep 15 '21 at 19:12