-5

I have a class. the constructor execute without problem. but WHY destructor does not perform after finishing program? I would appreciate for any help.

class foo{
    foo(){
   cout<<"This is constructor!"<<endl;
   }
   ~foo{
   cout<<"This is destructor! "<<endl;
   }
};

int main(){

foo* temp;
temp = new foo();

 /*some code*/

return 0;
}
Rommel
  • 1
  • 4

3 Answers3

2

Memory to the variables can be assigned in 3 ways. They are :

1) Automatic : A variable with automatic storage allocation will be destroyed after the loop gets closed.

2) Static : A variable with static storage allocation will be given memory before the program gets started and gets destroyed at the end of the program.

3) Dynamic : A variable with dynamic storage allocation gets its memory allocated by using new command and they can be deleted only by using its equivalent delete command.

Your problem is related to the third case in my above explanation. The memory is allocated using the new method , so just use delete at the position where you want to free its memory. If you have allocated the memory to the variable using the first case mentioned above, then the variable gets automatically destroyed at the end of the program.

Hope this helps you.

Rewanth Tammana
  • 1,453
  • 17
  • 20
1

Ther is nothing to deconstruct because you don't delete your pointer. Try this, it should deconstruct now.

class foo{
    foo(){
   cout<<"This is constructor!"<<endl;
   }
   ~foo{
   cout<<"This is destructor! "<<endl;
   }
};

int main(){

foo* temp;
temp = new foo();

 /*some code*/

delete temp;       // Deconstruct when pointer is deleted
return 0;
}
xxfelixxx
  • 6,512
  • 3
  • 31
  • 38
user3196144
  • 197
  • 2
  • 11
0

Since, you have allocated memory for your object dynamically (using new operator in your program), you must free it by using delete operator. By not doing so, you are creating memory leaks. That means, you need to keep track for memory being allocated by you and free it when you don't need them anymore. You can read about memory leaks over here.

However, for automatic objects, the destructor will be called implicitly when object goes out of the scope. For example,

int main()
{
    foo temp;
    // Do Something Interesting

    return 0;
}

The output of the above program will show you the Destructor being called.

Also, there are other problems with your program. One is you haven't declared the constructor and destructors for your class foo under Public lable. And second is you forgot a pair of parentheses with Destructor ~foo. The compiler will throw error if you try to compile your program. The correct program is given below:

#include <iostream>
using namespace std;

class foo {
public:
    foo() {
        cout<<"This is constructor!"<<endl;
    }
    ~foo() {
         cout<<"This is destructor! "<<endl;
    }
 };

int main() {

    foo* temp;
    temp = new foo();

     /*some code*/

    delete temp;       
    return 0;
} 
abhiarora
  • 9,743
  • 5
  • 32
  • 57