Consequences of removing a pointer

Object *p = new Object();
delete p;

      

When I remove p, the object allocation on the heap is removed. But what exactly happens to p? Is it removed from the stack? or is it still on the stack and still contains the memory address where the Object was previously?

+3


source to share


5 answers


p

is still on the stack and contains the address Object

you just removed. You can reuse by p

assigning it to specify other highlighted data or NULL

/ nullptr

.



+8


source


p

is the variable on the right. Thus, the lifetime is determined at compile time, not at run time.



+2


source


What you got here is called a drooped pointer - a monster that you would normally like to avoid at all costs.

0


source


When you do delete p

. The memory marked p

is deleted.

delete

~ = destructor + release

Here delete

is just a term that indicates that the memory is freed. Doesn't affect the total memory of the OS or the variable itself p

. p

still points to memory that is now patched by the system and thus becomes a dangling pointer.

0


source


The pointer variable is retained, but its value is rendered invalid - anything with it other than assigning another valid pointer or null pointer gives undefined behavior. There is no guarantee that the pointer value will not change .

0


source







All Articles