2

I have a class myclass. In the main function, I create objects of type myclass in every iteration and want to delete them after each iteration. I have not created the objects dynamically. Can I delete the them explicitly because I don't need them after the iteration gets over.

class myclass{
    int value;
    void do_function();
};

int main()
{
    for(int i=0;i<count;i++)
    { 
        myclass obj;
        obj.do_function();
    }
}

The object obj is not needed after one iteration, but the memory is still there. How can I free that memory?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ankita Saha
  • 137
  • 3
  • 10

4 Answers4

8

You don't have to delete them explicitly, because as you have created them myclass obj; they are created on stack and deleted after each iteration.

When the program reaches the first curly brace after the instantiation on stack of an object it deletes it, in your case:

myclass obj;
obj.do_function();
} // Here the obj is deleted

Here are some examples and explanations of how stack is working, versus heap, to let you understand better when you need to free memory yourself, and when you don't.

Note: I have used notions of stack and heap only to suggest how objects are handled relatively to their lifetime like when an object from stack should be freed after it leaves the scope, and an object of heap lives until it's explicitly freed. As mentioned in comments, these notions are not considered in standard C++ because the programs can run in an environment which does not support these type of memory. Although the compiler respects these rules for a C++ program.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
2

myclass obj; will already be deleted automatically after each iteration of the loop.

How can I free that memory?

You don't need to.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

Short answer is that you don't need to.

Here's a quick example:

class myClass {
    public:
        myClass() { cout << "Hello!" << endl; }
        ~myClass() { cout << "Goodbye!" << endl; }
        void do_function() { cout << "Something" << endl; }
};

int main() {
    for (int i=0; i<3; ++i) {
        myClass obj;
        obj.do_function();
    }
}

Output:

Hello!
Something
Goodbye!
Hello!
Something
Goodbye!
Hello!
Something
Goodbye!

You are allocating obj on the stack. Whenever your program goes out of scope, the memory automatically gets released from the stack.

You create a scope for every iteration of a for loop. Or the inner curly brace:

for (int i=0; i<3; ++i) { <-- Right there

And obj goes out of scope:

    obj.do_function();
} <-- Right here
Leumash
  • 43
  • 7
1

If you don't create an object dynamically, the object is created on stack and it is deleted when the closing bracket, that defines its scope, is seen. You can have your own opening and closing brackets to restrict the scope of a variable. This would be helpful to just free memory from unnecessary variables. For example

for(...)
{
    //do something
    {
        myclass obj;
        /// use obj here
    }//obj is deleted from stack
    //do something which doesnt require myclass obj
}
CODError
  • 779
  • 2
  • 10
  • 25