0

Why the delete operator malfunctions?

I created a pointer say 'p' and kept the addres of memory stored in heap in it and set its value to 5 using *p(dereferencing) Then i deleted it (pointer p - cleared the memory) But still it shows the same address of the memory it showed before deleting Next i create one more pointer q. And allocate some memory in heap and put its addres in q and address is the same as the one in pointer p And also when i change the value of memory q points to(say to 82) and print the value of "*p" i get the same result as 82 But according to rule the memory shoul not now exist with pointer p but here both pointers are using the se block of memory. Whats the problem Is ot a glich in sololearn's emulator( i have not yried it on my pc)

4th Jan 2020, 3:02 PM
Sajal Chuttani
Sajal Chuttani - avatar
2 Answers
+ 2
Not sure if I understand. When using delete on pointer, it deletes memory stored in heap, but your pointer still exists in stack, and points to this memory until it is destroyed or assigned new address.
4th Jan 2020, 3:26 PM
Null
Null - avatar
+ 1
Thanku Ales Gajdacz
4th Jan 2020, 3:48 PM
Sajal Chuttani
Sajal Chuttani - avatar