GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Oct-2007, 13:59
scherzo scherzo is offline
New Member
 
Join Date: Oct 2007
Posts: 2
scherzo is on a distinguished road

Why the destructor is called?


Hello everyone!

I'm coding an application and I don't understand why it isn't working. I've written a simple example to show you my problem:

CPP / C++ / C Code:
#include <iostream>
using std::cout;

class test {
friend test operator+(test&, test&);

public:
test() {}
test(const int& s1, const int& s2);
double& operator ()(int i, int j) {return v[i][j];}
~test();

protected:
double** v;
int _s1, _s2;
};

int main() {
test t(4, 4);
test a(4, 4);
test b(4, 4);

for (int i = 0; i < 4; i++) {
a(i, i) = i;
b(i, i) = i + 2;
}

t = a + b;
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++)
cout << t(i, j) << " ";
cout << "\n";
}

system("pause");

return 0;
}

test operator+(test& a, test& b)
{
test z(a._s1, a._s2);
for(int i = 0; i < a._s1; i++){
for (int j = 0; j < a._s2; j++)
z(i, j) = a(i, j) + b(i, j);
}

return z;
}

test::test(const int& s1, const int& s2) {
v = new double*[s1];
for (int i = 0; i < s1; i++)
v[i] = new double[s2];

_s1 = s1;
_s2 = s2;

for (int i = 0; i < s1; i++){
for (int j = 0; j < s2; j++)
v[i][j] = 0.0;
}
}

test::~test() {
for (int i = 0; i < _s1; i++)
delete [] v[i];
delete [] v;
}


The problem is that when I do "t = a + b;" the "return z" in the function "test operator+(test&, test&);" it calls the destructor for z before it returns and this makes a crash because the variable t doesn't have anything.

Also, the destructor for "t" is called right after the return from the function causing another crash.

The interesting part is that if I adapt the code to sum vectors intead of matrices, it works!

Any help would be appreciated.

Thanks,

scherzo
Last edited by LuciWiz : 22-Oct-2007 at 14:27. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 22-Oct-2007, 16:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Why the destructor is called?


Quote:
Originally Posted by scherzo
CPP / C++ / C Code:

t = a + b;



The problem is that when I do "t = a + b;" the "return z" in the function "test operator+(test&, test&);" it calls the destructor for z before it returns and this makes a crash because the variable t doesn't have anything.

The destructor is called when the + operator finishes, since the "test" object (a local variable) is no longer in scope. That's the way it works. The operator returns the object which contains a pointer to memory that is no longer valid.


Since you don't have an overloaded assignment operator, the assignment statement just copies the value of the pointer, v, that the + operator used, which is no longer valid. Therefore the crash.

Whenever you have a constructor that allocates storage you usually need an overloaded assignment operator and a copy constructor to take care of things like this.

Regards,

Dave
  #3  
Old 23-Oct-2007, 06:04
scherzo scherzo is offline
New Member
 
Join Date: Oct 2007
Posts: 2
scherzo is on a distinguished road

Re: Why the destructor is called?


Thanks for all davekw7x, I solved my problem now.

Best regards,

scherzo
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
a tester class and then some. postage Java Forum 1 06-May-2006 16:48
1 line php cover function changes result of called programs schgid MySQL / PHP Forum 0 03-May-2005 11:15
How do I check whether the object q1 exists? ie: q1=new queue has been called? wc3promet C++ Forum 1 15-Oct-2004 10:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:02.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.