I have a matrix class with a memory deleting destructor and a multiplication operator overloaded. When it multiplies two matricies, it checks their sizes to see if they can be multiplied, then creates a new matrix object of appropriate dimensions to store the result, which it returns at the end.
It performs all the math perfectly well, as the debugger tells me when going through, but it runs the destructor of the local object "result" BEFORE it returns it (a copy of it). The copy that it returns is empty and crashes the program. This is my first time back in C++ after awhile, and from what I remember before, I had similar problems, though this is the first time I've caught it freeing before returning. If the "delete" statement are removed from the destructor, everything works fine.
I've run this through gcc and it works just as expected, but in VC++ 6.0 it crashes with the same code. Why does it destroy before returning?
Potentially relevant code:
----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
It performs all the math perfectly well, as the debugger tells me when going through, but it runs the destructor of the local object "result" BEFORE it returns it (a copy of it). The copy that it returns is empty and crashes the program. This is my first time back in C++ after awhile, and from what I remember before, I had similar problems, though this is the first time I've caught it freeing before returning. If the "delete" statement are removed from the destructor, everything works fine.
I've run this through gcc and it works just as expected, but in VC++ 6.0 it crashes with the same code. Why does it destroy before returning?
Potentially relevant code:
Code:
template <class itemType> class matrix
{
public:
// constructors/destructor
matrix( int rows, int cols ); // size rows x cols
matrix( int rows, int cols, const itemType & fillValue ); // all entries == fillValue
~matrix( ); // destructor
int print();
// assignment
matrix & operator = ( const matrix rhs );
matrix operator * ( const matrix rhs );
// accessors
int numrows( ) const; // number of rows
int numcols( ) const; // number of columns
int set(int i, int j, itemType k); // set value at i,j to k
itemType get(int i, int j) const; // get value at i,j
private:
int myRows; // # of rows (capacity)
int myCols; // # of cols (capacity)
itemType **myMatrix; // the matrix of items
};
template <class itemType>
matrix<itemType>::matrix(int rows,int cols,const itemType & fillValue )
{
int i,j;
myRows = rows;
myCols = cols;
myMatrix = new itemType *[myRows];
for(i=0;i<myRows;i++)
{
myMatrix[i] = new itemType[myCols];
}
for(i=0;i<myRows;i++)
for(j=0;j<myCols;j++)
myMatrix[i][j] = fillValue;
}
template <class itemType>
matrix<itemType>::~matrix ()
{
int i;
cerr << "I'm killing stuff..." << endl;
for(i=0;i<myRows;i++)
{
delete [] myMatrix[i]; //if removed, works fine
}
delete [] myMatrix; //if removed, works fine
}
template <class itemType>
matrix<itemType> matrix<itemType>::operator * ( const matrix<itemType> rhs )
{
// check multiplicability
if(myCols != rhs.myRows)
{
cerr << "Dimension mismatch, cannot multiply" << endl;
exit(1);
}
//make new matrix of correct size, initializing everything to zero
matrix result(myRows,rhs.myCols,0);
int i,j,k;
for(i=0;i<myRows;i++) //each row in lhs
{
for(j=0;j<rhs.myCols;j++) //each col in rhs
{
for(k=0;k<myCols;k++) //each element in rhs row and lhs col
{
result.set(i,j,(myMatrix[i][k] * rhs.myMatrix[k][j])+result.get(i,j));
}
}
}
return result;
}
...but I'm just a C man trying to see the light