0

Please have a look at this below code::

    #include<iostream>
    using namespace std;
    
    int main()
    {
         int *ptr;
    
        ptr = new int[100000];
    
        cout<<"\n\n address = "<<ptr;

        for(long int i=0;i<100000;i++)
            *(ptr+i) = i+1;
    
        delete []ptr;
        cout<<"\n\n address = "<<ptr;

        ptr = new int[5];
        cout<<"\n\n address = "<<ptr;
        
        return 0;
    }

OUTPUT is:

address = 0xd42490

 address = 0xd42490

 address = 0xe919d0

Why,After delete []ptr; it is still pointing to its previous location(See the 2nd output).

anonymous
  • 13
  • 5
  • 2
    "as it should"? Who ever said `delete []ptr;` should do anything to the address value in `ptr` itself? – TheUndeadFish Aug 22 '20 at 17:31
  • 4
    Why do you believe the result should be something different? – Sam Varshavchik Aug 22 '20 at 17:31
  • 1
    delete doesn't change value of pointer but deallocate it and you can't change value of pointed memory after that. – Ali Askari Aug 22 '20 at 17:31
  • better read docs https://learn.microsoft.com/en-us/cpp/cpp/delete-operator-cpp?view=vs-2019#:~:text=When%20delete%20is%20used%20to,after%20the%20object%20is%20deleted. – Nikhil Badyal Aug 22 '20 at 17:33
  • 1
    Looks normal to me. When you call `delete[]` -- that memory you had a valid right to access is simply released for re-use by your program. Nothing happens to the address held by the pointer. It doesn't disappear and it's not marked bad -- it's just a memory location. You can still check the address -- but you can no longer access what is there. When you call `new` again, a new valid region of memory is reserved for your use and your pointer is updated to reflect the starting address for that region -- all normal. – David C. Rankin Aug 22 '20 at 17:35
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Aug 22 '20 at 17:35

2 Answers2

5

delete[] is working just as it should. It ensures that destructors are called for the members of the array and marks the memory (internally) as available for re-use.

There's nothing in the C++ standard that says it has to zero the memory or return the memory to the OS or clear the variable holding the address you passed to it, etc.

Once you delete something, you know destructors have run and you also know that you are no longer allowed to look at (access) that memory. If you do anyway, there's no guarantee what you'll get (except UB).

A pointer is just like a cardboard sign saying "there's something over there". When you use delete on the pointer it destroys what's "over there", but it doesn't re-write the sign itself (the pointer), so the pointer still points to the same memory location it always did, but accessing that memory location is now forbidden.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

delete [] has destroyed the array created dynamically at address ptr, but has not deleted the pointer itself.

A ptr = nullptr; command would perform that job, by indicating that ptr now points to nothing.

Giogre
  • 1,444
  • 7
  • 19