By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,404 Members | 933 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,404 IT Pros & Developers. It's quick & easy.

any suggestion please

P: n/a
hello C++ gurus,

I am having a problem and want help fom you.
Here is my problem-------------

I am designing a class for matrix which dynamically creates a 2d array
to hold data and have its binary overloaded operators like +,-,* etc.
Here is my codes:

class matrix
{
public:
matrix(int r, int c);
~matrix();
matrix& operator = (const matrix& src); //assignment operator
matrix operator + (const matrix& x); //addition operator

private:
int row;
int column;
int** data;

};

matrix::matrix(int r, int c)
{
//assign row and columns
row = r;
column = c;
//now allocate memory for data
data = new int* [row];
for(int i = 0; i < column; ++i)
data[i] = new int[column];

}

matrix::~matrix()
{
//we need to free the memory allocated earlier
if( data != NULL)
{
for(int i = 0; i < row; ++i)
delete[] data[i];
delete []data;
}
}

matrix matrix::operator + (const matrix& x)
{
matrix temp(row,column);

//add 2 matrices and store the result into temp

return temp;
}

If I want to add 2 matrices as

matrix a(2,2), b(2,2), c(2,2);
//initialise a,b with data
c = a + b;

then the following sequence of function calls happens:
1. firstly invokes matrix::operator + (matrix& x).
2.Just after "return temp;" statement of +operator(), destructor for
temp is called which deallocates the memory for temp.
3.Then assignment operator for "c = a + b;" statement is called which
tries to assign data from "temp" to "c". But here it fails due to
unavailability of "temp" memory.

Interestingly if I do not allow to delete memory in destructor then it
works fine. Also if I use stack memory for the 2d array(do not allocate
memory dynamically, that means say int data[3][3] ) then also fine.
I do not want to solve this problem by using a 2nd parameter of type
matrix and passing "c" in this parameter.
any suggestion in this regards will be most welcome.

thanks

bisuvius

Nov 8 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
bisuvious wrote:
I am designing a class for matrix which dynamically creates a 2d array
to hold data and have its binary overloaded operators like +,-,* etc.
Here is my codes:

class matrix
{
public:
matrix(int r, int c);
~matrix();
matrix& operator = (const matrix& src); //assignment operator
matrix operator + (const matrix& x); //addition operator

private:
int row;
int column;
int** data;

};
[snipped implementation]
>
If I want to add 2 matrices as

matrix a(2,2), b(2,2), c(2,2);
//initialise a,b with data
c = a + b;

then the following sequence of function calls happens:
1. firstly invokes matrix::operator + (matrix& x).
2.Just after "return temp;" statement of +operator(), destructor for
temp is called which deallocates the memory for temp.
Right. Also the copy constructor matrix::matrix (const matrix&) is
called to copy the result to the temporary that will live outside the
call a + b to be assigned to c. Although you didn't supply such a copy
constructor, there is one, e.g. the one that gets generated by the
compiler. This automatically generated copy constructor will copy the
class bit for bit, which means that you copy a pointer that already got
deleted. Hence the crash.
3.Then assignment operator for "c = a + b;" statement is called which
tries to assign data from "temp" to "c". But here it fails due to
unavailability of "temp" memory.

Interestingly if I do not allow to delete memory in destructor then it
works fine. Also if I use stack memory for the 2d array(do not allocate
memory dynamically, that means say int data[3][3] ) then also fine.
You are fine only out of coincidence. Provide a copy constructor and you
have done this thing properly.

Regards,
Stuart
Nov 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.