what does "delete" do for pointers?

Sep 8, 2019 at 4:11am
I am new to the pointer, I am so confused about the "delete" operator.
I understand that *ptr1=15 before I delete it.

Then after "delete ptr1", why the *ptr1 output the same thing (15) again? It should have been deleted, why the value doesn't change?

Thanks for answering!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
    int* ptr1 = new int(15);

    cout << "Value of ptr1, before delete: " << *ptr1 << endl;

    delete ptr1;    // Destroying ptr1

    cout << "Value of ptr1, after delete:  " << *ptr1 << endl;

    return 0;
}
Sep 8, 2019 at 4:21am
delete does not (necessarily) erase the value pointed to by the pointer. It instead calls the destructor of the pointed-to variable (if there is one; there isn't for ints) and releases the region of memory allocated by new back to the OS (conceptually, anyway).

Important: you access a pointer after deleting it. DO NOT DO THIS. This is a common memory management mistake. Once you call delete with a pointer, your program no longer has the right to use the memory the pointer points to.

-Albatross
Sep 8, 2019 at 5:00am
Thanks, Albatross! That was very clear.
Sep 8, 2019 at 6:56am
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; 
Sep 8, 2019 at 8:29am
you access a pointer after deleting it.

The Windows C/C++ runtime in Visual Studio 2019 won't allow that, the program terminates abnormally when trying to access a deleted pointer.

I remember it was possible without a crash in VS 2017 to access released memory.

<edited to add:>

I misrembered, or MS changed VS 2017 recently. Compile the following in either 2017/2019 and the program crashes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using USHORT = unsigned short;

int main()
{
   USHORT* pUShort { new USHORT { 10 } };
   std::cout << "*pUShort: " << *pUShort << ",\t\tpUShort: " << pUShort << "\n";

   delete pUShort;

   long* pLong { new long { 90000 } };
   std::cout << "*pLong:   " << *pLong << ",\tpLong:   " << pLong << "\n\n";

   // uh oh, this was deleted!
   std::cout << "Reassigning *pUShort...\n\n";
   *pUShort = 250;

   std::cout << "*pUShort: " << *pUShort << ",\t\tpUShort: " << pUShort << "\n";
   std::cout << "*pLong:   " << *pLong << ",\tpLong:   " << pLong << "\n";

   delete pLong;
}
Last edited on Sep 8, 2019 at 3:59pm
Sep 9, 2019 at 12:28am
int pointer = 10;
{
int memory[100];
memory[pointer] = 1234;
} //<- this is the delete memory statement

cout << pointer; //ok. its just an integer, and we can print its value.
cout memory[pointer]; //no can do: memory was lost at the closing } scope

its pretty much the above... conceptually.
Topic archived. No new replies allowed.