
-----------------------------------
boogerlad
Mon Dec 12, 2011 10:56 pm

Deleting an object
-----------------------------------
[code]int x = 1;
vector * herp = new vector;
vector derp;
delete x;
delete herp;
delete derp;[/code]

The program runs and compiles, but I get a warning
warning: the address of 'derp' will never be NULL

What does that warning mean? It just doesn't make any sense to me. If I can delete x, then why can't I delete derp without the warning?

-----------------------------------
Tony
Mon Dec 12, 2011 11:18 pm

RE:Deleting an object
-----------------------------------
it doesn't make sense to delete items on the stack. You can't delete x either.

tony$ cat test.cc
#include 
int main(){
   int x = 1;
   delete x;
   return 0;
}

tony$ g++ test.cc
test.cc: In function ?int main()?:
test.cc:4: error: type ?int? argument given to ?delete?, expected pointer


-----------------------------------
boogerlad
Tue Dec 13, 2011 12:06 am

Re: Deleting an object
-----------------------------------
sorry, I meant this:
[code]
    delete &derp;
    delete &x;
[/code]
deletion of x doesn't result in a warning, but derp does.

-----------------------------------
Tony
Tue Dec 13, 2011 12:15 am

RE:Deleting an object
-----------------------------------
No. You cannot delete objects from within the stack, even if you try. If you have to ask why, then you need to review how stack memory works.

Edit: it might not give you a compile-time exception (static analysis is hard), but it will break at run-time.

-----------------------------------
md
Tue Dec 13, 2011 12:17 am

RE:Deleting an object
-----------------------------------
Why you get that specific warning I'm not sure but really doing things like that *will* cause trouble.

-----------------------------------
boogerlad
Tue Dec 13, 2011 12:21 am

RE:Deleting an object
-----------------------------------
Fair enough. That might explain those random crashes. Sorry for the noob questions, I just started learning c++. I just came from java. Deleting the herp object is okay, because I used the new keyword correct? Anything that uses the new key word must be deleted manually?

-----------------------------------
md
Tue Dec 13, 2011 2:07 am

Re: RE:Deleting an object
-----------------------------------
Fair enough. That might explain those random crashes. Sorry for the noob questions, I just started learning c++. I just came from java. Deleting the herp object is okay, because I used the new keyword correct? Anything that uses the new key word must be deleted manually?

In a word, yes. Herp can be deleted because it was allocated from the heap (hence why you have a pointer to it). Derp and x are both allocated on the stack. In Java *every* object is allocated from the heap, only references and basic numeric types go on the stack; hence why you *always* use new to create new objects. With C++ you can create objects by allocating memory from the heap with new *or* you can create objects on the stack by simply declaring a variable. Things on the stack get automatically destroyed when the stack frame closes (not always the end of a function), things allocated from the heap must be deleted manually.

In your original post what you were doing was attempting to delete stack-allocated objects from the heap - which clearly makes no sense. In fact, it's an undefined operation IIRC and might do any number of things depending on the specific compiler you're using.
