What happens to an address after delete operator has been applied to it in C++?
Asked Answered
C

4

6

If I delete a pointer as follows for example:

delete myPointer;

And, after that did not assign 0 to the pointer as follows:

myPointer = 0; //skipped this

Will myPointer be pointing to another memory address?

Civic answered 14/2, 2011 at 9:17 Comment(1)
@user588855: assigning 0 to a deleted pointer is a controversial subject, depending on the situation, as it might hide structural bugs in the flow / memory handling. Better use smart pointers / containers and not have to call delete at all.Sling
D
18

No, in most implementations it will store the same address as previously - delete usually doesn't change the address and unless you assign a new address value it remains unchanged. However this is not always guaranteed.

Don't forget, that doing anything except assigning a null pointer or another valid pointer to an already deleted pointer is undefined behavior - your program might crash or misbehave otherwise.

Dolliedolloff answered 14/2, 2011 at 9:17 Comment(0)
U
5

myPointer would be pointing to the same memory address. But, it wouldn't be valid for you to use the memory at that address because delete would have given it back to the runtime/operating system, and the operating system my have allocated that memory for use by something else.

Unbridle answered 14/2, 2011 at 9:21 Comment(3)
So, what is it likely that I can use the deleted pointer with the same address it was pointing (allocating) at? Is it immediately that once the pointer is deleted it is given back to the OS for example or for something else?Civic
@user588855: Maybe it's returned to the OS, maybe not, maybe some other code (on another thread) asks for memory and obtains memory at that address. Whatever happens it'll likely be a lot of of pain for you - don't do that, use pointers and dynamic memory allocation properly.Dolliedolloff
The pointer is never given back to the OS. Instead, the block of memory that the pointer points to is given back to the OS. You don't delete pointers, you delete the memory to which the pointer pointsAgostino
L
3

Definetly, no. The delete operation doesn't change the pointer itself - it frees the memory addressed by that pointer.

Lapwing answered 14/2, 2011 at 9:21 Comment(1)
delete x is allowed to change the value of x: #5002555Agostino
K
0

This question is important! I have seen that Visual Studio 2017 has changed pointer value after "delete". It coused a problem because I has using memory tracing tool. The tool was collecting pointers after each operator "new" and was checking them after "delete". Pseudo code:

Data* New(const size_t count)
{
    Data* const ptr(new Data[count]);
    #ifdef TEST_MODE
    MemoryDebug.CollectPointer(ptr);
    #endif
    return ptr;
}

void Delete(Data* const ptr)
{
    delete[] ptr;
    #ifdef TEST_MODE
    MemoryDebug.CheckPointer(ptr);
    #endif
}

This code works good on Visual Studio 2008 but was failing on Visual Studio 2017 so I have changed the order of operations in second function.

However the question is good and the problem exists. Experienced engineers should be aware of that.

Karolinekaroly answered 28/7, 2018 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.