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,438
  • 68
  • 55
  • 126
Simplicity
  • 44,640
  • 91
  • 243
  • 375
  • 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

5 Answers5

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
  • 163,328
  • 92
  • 501
  • 942
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
  • 56,006
  • 37
  • 126
  • 199
  • 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,561
  • 3
  • 24
  • 36
  • `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.

0

Not always but it can change. In designing a hash table , I found the address to have changed after around 30 error-free tests on 1000 objects. It changed exactly after a delete operation. Solution: use T_ *const to store a side address to the object that will be deleted where T_ is pointer type and the const qualifier to ensure the address doesn't change.

Anil Myne
  • 69
  • 3
  • 1
    I'm not sure what you mean by "error free". Are you saying you did not fault when accessing invalid memory? Or the operations accessed freed memory, which is invalid, but did not flag an invalid result based on the test? Or perhaps you are saying that you transferred ownership from the table to a client? The answer is not clear. Also, smart pointers are preferable (as noted in prior comments). A case could be made for when smart pointers are not available (straight C?) - but your answer needs to be clarified. Thanks. – natersoz Nov 07 '20 at 15:57