llakais
llakais

Reputation: 1637

In C++, if an object is created within a method, is the memory used by the object automatically released after the method finishes?

In code I'm working on right now I have a method belonging to a class that itself creates an instance of another object to use within the method. Does the memory belonging to that object get automatically get released after the method returns and the object looses scope? Or will I be taking up more and more memory each time the method is called?

The code has this structure:

int Class::method(int input) {
    Other_Class local_instance;
    int i;

    i = local_instance.do_something();
    i *= input;

    return i;
}

So will the memory belonging to local_instance be released upon returning from the method? Or will I have many instances of Other_Class clogging up memory?

Thanks very much for your time and help!

Upvotes: 0

Views: 180

Answers (6)

Ian Haggerty
Ian Haggerty

Reputation: 1751

In addition to all the above, if the constructor for the object local_instance instantiates objects using the new operator to which are pointed to by a Other_Class* pointer. Then that object, upon destruction, will not automatically release the memory allocated in these operations. i.e. You have to think about the children as well! (think auto pointers, deletion in destructor's, etc).

Upvotes: 0

fdh
fdh

Reputation: 5344

In C++, unless the object is created on the heap, all memory is automatically deallocated. For example, Other_Class is created on the stack, rather than the heap therefore Other_Class will automatically be deallocated by the function returning.

However, objects on the heap will NOT be automatically deallocated. Instead it's the developers responsibility to clean up any memory on the heap.

For example, although your code is fine, this code creates a memory leak:

int main ()
{
   Other_Class *memOnHeap = new Other_Class;
   return 0;
}

In the code above, gcc will allow it to compile however, you will create a memory leak the size of a Other_Class object because you have allocated memory for it on the leak, but not deallocated with a call to delete. The problem can be easily fixed by inserting a delete memOnHeap; right before your return main.

Upvotes: 1

celtschk
celtschk

Reputation: 19721

Well, to begin with, local_instance is not an object of class Other_Class but a function without arguments returning Other_class. This is also known as C++'s most vexing parse.

EDIT: The code in the question has been corrected; the original version had

Other_Class local_instance();

But let's assume that this line actually read

Other_Class local_instance; // no parentheses

Then yes, this object will be automatically get released at the end of the function. Note however that the same is not true for objects you allocate with new, for those objects it is your responsibility to delete them when no longer needed.

Upvotes: 1

Christopher
Christopher

Reputation: 774

Assuming Other_Class::~Other_Class() politely cleans up any memory Other_Class allocates on the heap.

Upvotes: 1

antlersoft
antlersoft

Reputation: 14786

Unless the object is created on the heap with the new operator, the object will be destroyed and memory reclaimed when it goes out of scope.

Upvotes: 0

kol
kol

Reputation: 28688

The local_instance object will be allocated on the stack, and it will be destroyed (its destructor will be called) when the method returns.

Upvotes: 3

Related Questions