Page 1 of 1

Destructor call problem.

Posted: Fri Apr 16, 2010 6:46 pm
by SukhdeepMankoo
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

Re: Destructor call problem.

Posted: Sat Apr 17, 2010 2:03 am
by Neo
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.

Re: Destructor call problem.

Posted: Mon Apr 19, 2010 3:44 pm
by SukhdeepMankoo
Thanks Neo.