DMA using new[] and delete[]

I've been trying to get the hang of dynamically allocated memory in C++. To test things, I created a simple program that dynamically allocates an array, assigns values to it and prints it, deletes it and then prints it again. I did this expecting the program to crash the second time it tried to print the values, but in fact it prints out the correct sequence of values again just fine. I find this puzzling as I thought the pointer should no longer point to the addresses of these values after using delete[].

Any ideas why this is happening?

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

using namespace std;

int main()
{
    int *ptr = new int[5];

    cout << "Before deleting array:\n";

    for(int i = 0; i < 5; ++i)
    {
        *(ptr + i) = i + 1;
        
        cout << *(ptr + i) << endl;
    }

    delete[] ptr;

    cout << "\nAfter deleting array:\n";

    for(int i = 0; i < 5; ++i)
    {
        *(ptr + i) = i + 1;
        
        cout << *(ptr + i) << endl;
    }

    return 0;
}
Trying to deference a deleted array is undefined behavior - it could work, or it could cause your program to do anything else. The delete/delete[] operation basically tells your Operating System that its okay to reallocate the freed memory somewhere else, but the OS doesn't have to overwrite the data until it needs to.
delete doesn't actually delete memory. The memory still exists, your program just doesn't have it allocated anymore, which means it is free to be allocated elsewhere.


It's like the hotel room analogy:

When you check into a hotel room (new), you are given a key (a pointer).
When you check out (delete), you can still keep the key/pointer if you want.

And you can even try to visit the room later. And in fact, the room might be just as you left it. Or someone else might be in there. Or it might have been turned into a janitor's closet.
Yes that is the analogy I was referring to
What I don't understand is, if using delete[] de-allocates the memory the pointer is pointing to, how can the pointer still access it?

Is it that the pointer is no longer associated with the block of memory it was pointing to, but for at least the scope of the function its in, it can still be used to access and modify the memory, albeit illegally?
Last edited on
Allocating a memory is claiming it. It is like saying, "this is mine, do not give it away to something else". When you deallocate, you are saying that it is free and can be reused. It does not mean that system should instantly zero all memory. When bell rings, you do not expect that blackboard in class will magically clean itself, do you? If you come back after classes you might see writing still here (memory was not overwritten — your case), you can see clean board or somebody else writings (somebody reclaimed memory and used for his needs) or step in bear trap somebody placed here (system decided to remove this page from process address space as it is completely free and now access result in segmentation fault).

C++ does not prevents you from accessing some completely arbitrary memory area, it just says that everything beyound defined behavior is not supported and anything can happen.
Last edited on
Thank you MiiNiPaa, but I know that de-allocating memory frees it up for use in the system and that just because it was de-allocated it doesn't mean the values at the memory addresses are instantly changed/reset.

What I'm asking is, how is it that the pointer, once it no longer points to the memory, can still access it and display it as though it still held the memory? In my program the pointer no longer has anything to do with the memory block, yet seemingly it somehow still knows how to access the memory.
Certain regions of memory are accessible to your program at all times. Note that "accessible" does not mean "allocated".

What I'm asking is, how is it that the pointer, once it no longer points to the memory,


It still points to the memory. delete[] does not do any of the following:

- It does not change the address that is contained in the pointer
- It does not make the memory at that address stop being memory
- It does not make the memory at that address inaccessible.

It only de-allocates the memory. But your pointer still points to it... and the memory is still memory... and the memory is still accessible.

Again it's like the hotel example. You still have a key to the room, and the room is still there... but if you try to go in it after you check out of the hotel you might be barging in on someone else who's staying there (or you might not).
Last edited on
once it no longer points to the memory
A pointer does not point to anything. It contains some number, namely memory address which is used when dereferencing it to retrieve data contained at that address. delete does not change that value: it manipulates memory, not pointer.

As long as you do not change your pointer itself, it will continue to contain same value and you can use it to retrieve values stored at that memory.

You can even create pointers from thin air:
1
2
3
4
5
6
int main()
{
  int* x = (int*)0x230000;
  std::cout << *x;
// or even *reinterpret_cast<double*>(0x22efff);
}
(Program might crash. Or not. It depends)
Okay, so when the pointer is allocated memory, it has full control over that memory. It is given the memories location, and it can access and modify that memory as much as it wants without repercussion (for the sake of argument).

Then, when that memory is de-allocated, its not taken away or hidden from the pointer, it's simply no longer under the legitimate control of the pointer. The pointer still points to the memory location, but it is unsafe for the pointer to act on this memory, as that memory may now be in use by another part of the program or by another program altogether.

Attempts to modify such memory using the pointer would be dangerous, as you may be changing just about anything in the system, including for example the information needed to start up the machine.

Is that at least a decent-ish understanding of how it works?
Is that at least a decent-ish understanding of how it works?


More or less, yeah.


Just replace "pointer" in most of your text with "program" or "process". A pointer is just a container to hold an address -- it doesn't actually own anything or do anything special. Your program is what allocates memory, not the pointer. The pointer just holds the address of that memory.
Thanks for clearing that up guys, you've been a great help as ever.
Topic archived. No new replies allowed.