Destructor call problem.

C, C++, Visual C++, C++.Net Topics
Post Reply
SukhdeepMankoo
Lieutenant
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Destructor call problem.

Post by SukhdeepMankoo » Fri Apr 16, 2010 6:46 pm

hi,
I have written the following code.

Code: Select all

#..........

class test
{  public:
   static unsigned char temp;
   test()
   { temp++;
   }
   ~test()
  {  temp--;
  }
};

test f(test t)
{ return t;
}
unsigned char test::temp=0;

int main()
{  class test o1;
   class test o2=f(o1);
}
during debugging above code, i have found that

class test o1-- temp=1 as object is created.
class test o2=f(o1) temp=0xff as destructor is called two times.

I do not understand, why destructor is called two times.

kindly tell me, why this program behaves like this, according to my knowledge, output should be
during
class test o2=f(o1) temp=0;
kindly tell me why this difference comes.

Thanks & Regards
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Destructor call problem.

Post by Neo » Sat Apr 17, 2010 2:03 am

On mine it works perfectly...After execution, temp est at 0 as expected.

One thing to note. I do not recommend writing classes for DSP applications. It is very inefficient and hard to handle especially when it comes to optimisation stage where we need to replace some C routines with assembly. Another reason is classes use dynamic memory. In DSPs, dynamic memory is not efficient as in PCs. For DSPs, write C/C++ codes like assembly (without classes).

For PCs, classes are fine.
SukhdeepMankoo
Lieutenant
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Re: Destructor call problem.

Post by SukhdeepMankoo » Mon Apr 19, 2010 3:44 pm

Thanks Neo.
Post Reply

Return to “C/C++ Programming”