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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 5
    @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. – Matthieu M. Feb 14 '11 at 09:22

4 Answers4

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.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
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.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • 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? – Simplicity Feb 14 '11 at 10:03
  • 3
    @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. – sharptooth Feb 14 '11 at 10:30
  • 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 points – Aaron McDaid Jul 20 '15 at 19:21
3

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

grzkv
  • 2,599
  • 3
  • 26
  • 37
  • `delete x` is allowed to change the value of `x`: http://stackoverflow.com/questions/5002055/is-the-pointer-guaranteed-to-preserve-its-value-after-delete-in-c/5002201#5002201 – Aaron McDaid Jul 20 '15 at 19:33
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.