Computer Science Canada Deleting an object |
Author: | boogerlad [ Mon Dec 12, 2011 10:56 pm ] | ||
Post subject: | Deleting an object | ||
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? |
Author: | Tony [ Mon Dec 12, 2011 11:18 pm ] |
Post subject: | RE:Deleting an object |
it doesn't make sense to delete items on the stack. You can't delete x either. Quote: tony$ cat test.cc #include <new> 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 |
Author: | boogerlad [ Tue Dec 13, 2011 12:06 am ] | ||
Post subject: | Re: Deleting an object | ||
sorry, I meant this:
deletion of x doesn't result in a warning, but derp does. |
Author: | Tony [ Tue Dec 13, 2011 12:15 am ] |
Post subject: | 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. |
Author: | md [ Tue Dec 13, 2011 12:17 am ] |
Post subject: | RE:Deleting an object |
Why you get that specific warning I'm not sure but really doing things like that *will* cause trouble. |
Author: | boogerlad [ Tue Dec 13, 2011 12:21 am ] |
Post subject: | 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? |
Author: | md [ Tue Dec 13, 2011 2:07 am ] |
Post subject: | Re: RE:Deleting an object |
boogerlad @ 2011-12-13, 12:21 am wrote: 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. |