This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Let us say that an object has been allocated memory in a function. Now, when does that memory free up?
Is it something that gets freeded up as soon as the method exits or is it something that needs to be deallocated explicitly?..
Well, it depends. If it is a local variable, then it will be freed when it goes out of scope. If it is a variable that has been assigned via the new operator (ie. a pointer variable), then it will be freed when the delete operator is called.
Great question. It's basically been answered here....
(1) For local (that is, automatic variables, allocated on the stack), they are destroyed when the associated variable goes out of scope.
(2) If they are allocated with "new", then you must explicitly delete them.
(3) For global/static variables, the objects are destroyed when the program ends.
(4) Otherwise, all objects are supposed to be de-allocated (by default) when the program ends. I'm not sure you should always count on this, however. I would still take special care to explicitly delete objects allocated with "new."