I was wondering where objects instantiated inside a local method stored? As far as my knowledge goes, objects are stored in the heap, and their references are stored in the stack. If so, then when the function returns,and the reference to the object no longer exists (since the local stack frame of the function is popped), does the object stay in the heap, or do we have to delete it manually (or using garbage collection, as in Java)?
Asked
Active
Viewed 2,685 times
2
-
1Are you asking about C++ or Java? Pick one, and remove the other tag from your question. C++ objects and Java objects are fundamentally different. An answer that applies to one will not apply to the other. Don't spam tags. – Sam Varshavchik Dec 02 '16 at 12:00
-
Getting an answer to both would be helpful. – Prashant Pandey Dec 02 '16 at 12:03
-
You should read http://stackoverflow.com/questions/5836309/stack-memory-vs-heap-memory – Ol1v3r Dec 02 '16 at 12:04
-
http://stackoverflow.com/questions/10157122/object-creation-on-the-stack-heap – Richardson Ansong Dec 02 '16 at 12:07
-
for java: there is exactly 1 way to instantiate objects & regardless of where you do it and the result is always an object living in the heap which can not be manually deleted. References can also be stored in the heap: when objects refer to other objects (which I guess happens in C++ too) – zapl Dec 02 '16 at 12:50
1 Answers
2
It depends what you plan to do with that object, if you return the object then it's lifetime is extended. If you create it as a temporary then there are two possible outcomes.
If you Create the object with
new
. If you create a pointer to a new object then the pointer will be deleted when the method goes out of scope. But the object remains causing memory leaks, the object will need to be explicitly deleted.If you do not use
new
. The object will be deleted when the scope ends.
Both of these outcomes assume you do not return the object and you instantiated it as temporary object in the function.
Here is some sample code:
class ObjectClass {
public:
ObjectClass() {}
};
void myFunction() {
ObjectClass my_obj(); //memory is handled for you
ObjectClass * my_dynamic_obj = new ObjectClass();
delete my_dynamic_obj; //if delete is not called then
// the pointer my_dynamic_obj will be deleted but the object itself will remain
return;
}
main() {
myFunction();
return 0;
}

TomJ
- 424
- 3
- 14
-
-
Yes absolutely, just create them in the standard way. E.g. `ObjectClass my_obj()`. I'm assuming you've come from Java, using `new`can be dangerous as it can cause memory leaks if the object isn't deleted. Give me 10 minutes and I'll tidy up my answer once I get to a PC – TomJ Dec 02 '16 at 12:18
-
1I instruct my team to _never_ use `new` and `delete`. Automatic storage is always to be preferred. When you _really_ need dynamic storage use the right kind of smart pointer. There are obviously exceptions to every rule, but it takes an experienced developer to break the rule responsibly, and an even more experienced developer to break the rule correctly. – Allison Lock Dec 02 '16 at 13:38