class matrix
{
public:
int nrow;
int ncol;
double **m;
matrix(int r, int c)
{
nrow = r; ncol = c;
m = (double**)malloc(nrow*sizeof(double*));
for(int i=0; i<nrow; i++)
m[i] = (double*)malloc(ncol*sizeof(double));
for(int i=0; i<nrow; i++)
for(int j=0; j<ncol; j++)
m[i][j] = 0;
}
~matrix()
{
for(int i=0; i<nrow; i++)
{
printf("destructor loop: %d\n", i); fflush(stdout);
free(m[i]);
}
free(m);
}
matrix(const matrix& that)
{
this->nrow = that.nrow;
this->ncol = that.ncol;
this->m = (double**)malloc(nrow*sizeof(double*)); // dynamic mem allocation
for(int i=0; i<this->nrow; i++)
m[i] = (double*)malloc(ncol*sizeof(double));
for(int i=0; i<that.nrow; i++)
for(int j=0; j<that.ncol; j++)
this->m[i][j] = that.m[i][j];
}
void operator=(const matrix &that)
{
this->nrow = that.nrow;
this->ncol = that.ncol;
for(int i=0; i<nrow; i++) // clear current m
free(m[i]);
free(m);
this->m = (double**)malloc(nrow*sizeof(double*)); // dynamic mem allocation
for(int i=0; i<this->nrow; i++)
m[i] = (double*)malloc(ncol*sizeof(double));
for(int i=0; i<that.nrow; i++)
for(int j=0; j<that.ncol; j++)
this->m[i][j] = that.m[i][j];
}
void show()
{
printf(" Marix:\n");
for(int i=0; i<nrow; i++)
{
printf(" ");
for(int j=0; j<ncol; j++)
{
if(m[i][j] >= 0) printf(" ");
printf("%.6lf ", m[i][j]);
}
printf("\n");
}
printf("\n");
}
matrix cofactor(int r, int c) // returns cofactor of a[r][c]
{
printf("cofactor: size:(%d, %d) element:(%d, %d)\n", nrow, ncol, r, c); fflush(stdout);
matrix ans(nrow-1, ncol-1);
int x = 0, y = 0;
for(int i=0; i<nrow; i++)
{
if(i == r) continue;
for(int j=0; j<ncol; j++)
{
if(j == c) continue;
ans.m[x][y] = m[i][j];
y++;
}
x++;
}
return ans;
}
double det()
{
if(nrow != ncol)
{
printf("non-square matrix: (%d, %d)\n\n", nrow, ncol);
exit(1);
}
if(nrow == 1) return m[0][0];
if(nrow == 2)
{
double ans = m[0][0]*m[1][1] - m[1][0]*m[0][1];
return ans;
}
double ans = 0;
int sign = 1;
for(int i=0; i<nrow; i++)
{
printf("det[size:%d, %d] loop row: %d\n", nrow, ncol, i); fflush(stdout);
matrix a(2, 2);
a = cofactor(i, 0);
ans += (sign*a.det());
sign = sign*(-1);
}
return ans;
}
};
int main()
{
matrix c(3, 3);
c.m[0][0] = 2; c.m[0][1] = -3; c.m[0][2] = 1;
c.m[1][0] = 2; c.m[1][1] = 0; c.m[1][2] = -3;
c.m[2][0] = 1; c.m[2][1] = 4; c.m[2][2] = 5;
c.show();
printf("determinant: %lf\n", c.det());
return 0;
}
A segmentation fault occurs when calling the det function for even 3x3 matrices. i do not have experience with sophisticated debuggers but using printf statements it looks like the runtime error occurs during an iteration of the loop in the destructor. also i have tried using vector< vector< double >> instead of double**. i assumed it would not need a destructor in that case. then it is giving me a "double free error". i understand when it occurs but cannot find the error in my code. i have also tried calculating the cofactor inline instead of calling a function but it does not help.
int x = 0, y = 0;
for(int i=0; i<nrow; i++)
{
if(i == r) continue;
for(int j=0; j<ncol; j++)
{
if(j == c) continue;
ans.m[x][y] = m[i][j];
y++;
}
x++;
}
The logic for incrementing y
is broken here. Perform a simple mental experiment. If the original matrixes are 3x3, and they are being reduced here to a 2x2
matrix, your ans
, then you expect each value in ans
to be initialized here, and
ans.m[x][y] = m[i][j];
y++;
will get executed four times. Since y
is initially set to 0 and never gets reset to 0, the value of y
used in this assignment will range from 0 to 3, so, at some point, this will attempt to assign something to ans.m[x][3]
, which, of course, doesn't exist, and hillarity ensues.
It seems that the obvious intent here is to reset
y=0;
As the very first order of business in the outer for
loop, instead of just once, at the very beginning.