Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to the pointer itself after delete? [duplicate]

Tags:

c++

pointers

void doSomething()
{  
    TheObject *ptr = new TheObject;
    delete ptr;
    ptr = NULL;
    return 0;
}

Let me borrow the words from operator delete in cplusplus.com:

Deallocates the memory block pointed by ptr (if not null), releasing the storage space previously allocated to it by a call to operator new and rendering that pointer location invalid.

Please help to clear my confusions: what happens to the pointer itself after delete? The pointer itself does have an address, right? So after the pointed block is deleted, what about the pointer itself?

Could I say that the pointer itself will be free after returning the method where the pointer is initialized? Is the pointer itself placed on the stack or heap?

like image 693
Stephen Kuo Avatar asked May 13 '14 01:05

Stephen Kuo


People also ask

What happens when a pointer is deleted twice?

The answer is nothing happens.

Does delete remove the pointer?

delete keyword in C++ New operator is used for dynamic memory allocation which puts variables on heap memory. Which means Delete operator deallocates memory from heap. Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed.

Is it fine to delete twice for a pointer?

It is undefined behavior to call delete twice on a pointer. Anything can happen, the program may crash or produce nothing.


1 Answers

The pointer itself does have an address and the value. The address of the pointer does not change after you perform delete on it. The space allocated to the pointer variable itself remains in place until your program releases it (which it might never do, e.g. when the pointer is in the static storage area). The standard does not say what happens to the value of the pointer; all it says is that you are no longer allowed to use that value until you assign your pointer a valid value. This state of the pointer is called dangling.

In your program, the pointer ptr is dangling after delete has completed, but before the ptr = NULL assignment is performed. After that it becomes a NULL pointer.

The pointer it self is placed on stack or heap?

Pointer variable is a regular variable. Its placement follows the same rules as the placement of other variables, i.e. you can put a pointer in a static area, in an automatic area (commonly known as "the stack") or in the dynamic memory (also known as "the heap"). In this case, you allocate a pointer to a pointer:

TheObject **ptrPtr = new TheObject*; // The pointer to a pointer is on the stack
*ptrPtr = new TheObject;             // The pointer to TheObject is in the heap
delete *ptrPtr; // The space for TheObject is released; the space for the pointer to TheObject is not
delete ptrPtr;  // Now the space for the pointer to TheObject is released as well
// The space for the pointer to pointer gets released when ptrPtr goes out of scope
like image 89
Sergey Kalinichenko Avatar answered Sep 18 '22 12:09

Sergey Kalinichenko