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.