I'm trying to develop my own custom 'Matrix' class for use in pattern recognition and neural networks in C++. For the most part, it's working fine, but I'm running into an issue with my code when assigning variables. One method of assigning a variable is not working, and it seems to crash my code.
To give a little background, my matrix is an array of array of doubles within a class called Matrix. I've added appropriate constructors and destructors as well as a way to manipulate individual elements of a matrix. My main issue is assigning class variables. Say I have two matrices: A and B. The goal is to copy the contents from A to B. If I try one way, my code works as intended. If I try another way, my code crashes when trying to free after executing.
class Matrix{
public:
//Constructors
Matrix(); //EMPTY Matrix
Matrix(int, int); //Matrix WITH ROW AND COL
~Matrix(); //Destructor
void operator= (const Matrix &);
double & operator() (int X,int Y) const{return this->array[X][Y]; }
void print() const; //Print the Matrix
private:
double **array;
int nrows;
int ncols;
int ncell;
};
//When you want to copy a matrix to another Matrix variable
void Matrix::operator= (const Matrix &M) {
if(this->array != NULL){
for(int i=nrows-1; i>=0; i--) free(this->array[i]);
free(this->array);
this->array = NULL;
}
//Using parameters from the matrix being copied, rebuild it
this->nrows = M.nrows; this->ncols = M.ncols; this->ncell = M.ncell;
//First, create an ariray of double* for the rows
this->array = (double **) malloc(sizeof(double *)*(this->nrows));
//Next, go through each 'row', and copy over elements
for(int i=0; i<(this->nrows); i++){
this->array[i] = (double *) malloc(sizeof(double)*(this->ncols));
for(int j=0; j<(this->ncols); j++){
this->array[i][j] = M.array[i][j];
}
}
}
int main(int argc, char *argv[]){ //C.applyFunc(SP);
printf("\n\nCreating Matrix A\n");
Matrix A(1,3); A(0,0) = 8;
printf("\n\nPRINTING \n\n"); A.print();
printf("\n\nCreating B\n\n");
Matrix B = A; //THIS IS THE PROBLEM RIGHT HERE!!!
//Matrix B;
//B = A;
printf("\n\nPRINTING B\n\n"); B.print(); B(0,0) = 123;
printf("PRINTING A AGAIN\n\n"); A.print();
printf("PRINTING B AGAIN\n\n"); B.print();
return 0;
}
In my code, I've posted my class, the operator overloading of '=', and my main function. The other functions aren't as important, just printing the matrix and what not. If you find it helpful, I'll include it later. Here, in my main code, matrix A is being assigned as a 1x3 row matrix, setting A[0][0] to 8. Printing out the matrix verifies this. Now, when I assign B to A as shown in the line that's not commented out, I expect B and A to have the same values. Later, I change B[0][0] to 123.
At the end of it all, I expect A to be [8, 0, 0] and B to be [123, 0, 0]. However, when I print out A and B again, they are both the same, [8, 0, 0]. It seems that somehow, B points to A, so when A is freed after B, it's already freed and it crashes. However, when I run the commented code and assign B that way, my code works exactly as I expect it to. What exactly is happening when I call 'Matrix B = A' that makes it different than the commented code below it?
Well, in short,
Matrix B = A;
is not about executing assignment, but copy constructor, which, if I didnt miss something, is not defined in your class.