+ 1

Question related to the delete oprerator for a pointer

int *ptr=new int; *ptr=10; delete ptr; cout<<*ptr; still printing 10 as the output after i had freed the memory which has stored the value 10. i dont understand why?

12th May 2018, 6:55 AM
Aman Kumar Jain
Aman Kumar Jain - avatar
1 Odpowiedź
+ 2
Memory keeps the same values till it’s overwritten. The delete operator just frees the memory, it does not overwrite the memory. After deleting a pointer, dereferencing it is undefined behavior.
12th May 2018, 7:59 AM
aklex
aklex - avatar