I am trying a simple experiment with the C++ new and delete commands for dynamic memory. As the photo shows, the delete command is not working as expected. It seems to delete the value stored at the address pointed to by the pointer instead of deleting the link between the pointer and the address. After the delete command the dereferenced value is 0 but the address the pointer points to is still there. Then I am able to assign a new value to the pointer and print it as well as showing the same address is in use. Can anyone tell me what I am doing wrong? Thank you.enter image description here
Asked
Active
Viewed 39 times
0
-
2Please copy-paste code into the question. Do not post code in images. See [ask]. Please [edit] your question. – walnut Apr 15 '20 at 23:05
-
1[Related concept](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – user4581301 Apr 15 '20 at 23:12
-
I can't copy and paste your code into my IDE to run your program. Help stops here. – Thomas Matthews Apr 15 '20 at 23:43
-
***It seems to delete the value stored at the address pointed to by the pointer instead of deleting the link between the pointer and the address*** A call to delete tells the c++ run time that you no longer need to use the memory block you were given in the past by a call to new or new[]. The runtime may or may not return the memory block to the OS or depending on its size it may keep the block or some part of the block for a future allocation. Once you delete you are no longer permitted to look at the memory at that address. This is UB if you break the rules. – drescherjm Apr 15 '20 at 23:43