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

When I delete p, the object allocation on the heap is deleted. But what exactly happens to p itself? Is it deleted from the stack? or is it still in the stack and still contains the address of the memory that previously held Object?

TheNotMe
  • 1,048
  • 2
  • 17
  • 33

5 Answers5

8

p is still on the stack and holds the address of the Object you've just deleted. You are free to reuse p, assigning it to point at other allocated data or NULL / nullptr.

simonc
  • 41,632
  • 12
  • 85
  • 103
2

p is a variable, right. So it's lifetime is determined at compile time, not at runtime.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

What you have got here is called dangling pointer - a monster you normally would want to avoid at any cost.

KBart
  • 1,580
  • 10
  • 18
  • Except that the very act of saying `delete p;` is what creates that dangling pointer, cause the pointer's still there and you just invalidated its value. You can't avoid them altogether; all you can do is avoid *using* it. – cHao Feb 05 '13 at 15:00
  • Like what? If object is cleared orderly and pointer is used safely (i.e. checking if it's not NULL before using), I see no pitfalls here. – KBart Feb 05 '13 at 15:04
  • Of course wiki is not the most reliable source of information, but I use this practice for years and haven't noticed any problems - http://en.wikipedia.org/wiki/Dangling_pointer#Avoiding_dangling_pointer_errors. – KBart Feb 05 '13 at 15:07
  • @KBart: One issue is, `delete NULL;` does nothing. Meaning you have basically no chance of detecting a double `delete`, particularly if any other code has a copy of that pointer. If you don't want to keep track of your objects' lifetimes correctly, use a smart pointer instead. – cHao Feb 05 '13 at 15:10
  • @cHao the questions is about *pointer* itself, not about an object it is pointed to and related memory management. If doing it the way I described, your example would turn into *if(p) delete p;* – KBart Feb 05 '13 at 15:14
  • `if (p) delete p;` equates to `delete p;`. Like i said, deleting a null pointer does nothing. – cHao Feb 05 '13 at 15:15
  • The object being pointed at is at least as important as the pointer is, and the pointers to it are an integral part of proper memory management. You don't get to just handwave that away. – cHao Feb 05 '13 at 15:16
  • @cHao it does not, if *p* is equal to *NULL* the *delete p* statement will be skipped - that's why *if* is for. And of course I don't deny an importance about proper memory management, but **this** question and discussion is not about that - there are plenty of related topics around. – KBart Feb 05 '13 at 15:18
  • 1
    @KBart: Let me repeat: `delete NULL;` does nothing. So `if (p) delete p; /* else do nothing */` does exactly the same thing. – cHao Feb 05 '13 at 15:23
  • 1
    You brought memory management into the equation by even mentioning the evil of "dangling pointers". In the context of a single statement, the term itself is irrelevant. `delete p;` by its very nature *makes* `p` a dangling pointer, so you can't avoid dangling pointers "at any cost" and still use `delete` at all. As for the problem with dangling pointers? It lies *entirely* in the realm of memory management. So you don't get to say "dangling pointers are bad" and then say that memory management isn't part of the discussion. :P – cHao Feb 05 '13 at 15:33
  • @cHao, you said that `p=NULL` will hide other things. Like what? – Adri C.S. Feb 05 '13 at 15:52
  • 1
    @AdriC.S.: Double deletes, for example. If you delete a pointer and then automatically disavow all knowledge of it (by nulling it out), that complicates the problem of finding a rogue deleter. As a pathological case, `delete p; p = nullptr; delete p;` won't be noticed, whereas `delete p; delete p;` is more likely to cause problems. You could argue that one *wants* to avoid nasty side effects, but i'd argue that it's easier to notice problems when the code isn't conspiring to hide them. And it's easier to track them down when the offending code hasn't erased evidence of its involvement. – cHao Feb 05 '13 at 20:28
0

When you perform delete p. The memory pointed by p is deleted.

delete ~= destructor + deallocation

Here delete is just a term which states that the memory is released. There is no impact on total amount of memory of OS or the variable p itself. p still points to the memory which is now reclaimed by the system, and thus becomes a dangling pointer.

iammilind
  • 68,093
  • 33
  • 169
  • 336
0

The pointer variable persists but its value is rendered invalid - doing anything with it except assigning another valid pointer or a null pointer yields undefined behavior. There's no guarantee the pointer value is unchanged.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979