any suggestion please

B

bisuvious

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 = 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;
delete []data;
}
}

matrix matrix::eek:perator + (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::eek:perator + (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
 
S

Stuart Redmann

bisuvious said:
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::eek:perator + (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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top