Delete command "only" releases the memory allocated the pointer by the
new
command.
When the "new" command allocates a memory to some pointer in a program, it means that the memory allocated by new command will now be used by that program and no other software or program can use that memory as long as it is in use by this program.
However, once you use the
delete
command on that pointer, it releases that memory from your program. By releasing, it means that now any other program can "allocate" that memory to some other thing (it could be another pointer or anything)...
However, as long as that memory isn't allocated to some other program and that program doesn't change the value of this memory, the "value" in this memory will remain the same as you assigned it (in your case, 15) and because your pointer [ptr] is "still pointing" towards that same memory (even though it is released), your program outputs 15 even after release of the memory.
But you shouldn't be doing this. Such a pointer which points to an already released memory is known as "Dangling Pointer" (you should search on it, it's a very interesting and important concept)
To avoid such complication, always make your pointer equal to NULL after releasing the memory.
1 2
|
delete ptr1;
ptr1 = NULL;
|