Program crashes with SIGSEGV error when declaring custom matrix type
-
Hello,
I have an matrix calculator app, but in some cases when I am declaring matrix of custom data type my program crashes with SIGSEGV error. Now, this is happening only in function for calculating determinant of matrix, but not in others.
So, in this case everything is fine:Matrix Matrix::Multiply(float c) const { Matrix A; A.SetMatrixDimension(rows,cols); for(unsigned i=0; i<rows; ++i){ for(unsigned j=0; j<cols; ++j){ A.setValue(i,j, c * AccesValue(i,j)); } } return A; }
But here program crashes when declaring:
double Matrix::Determinant() const { unsigned N = cols; Matrix cofactors; //Qt creator debuger is indicating this line as origin of crash cofactors.SetMatrixDimension(N,N); double det = 0; int sign = 1; if(N==1){ return AccesValue(0,0); } for(unsigned i=0; i<getCols(); ++i){ for(unsigned j=0; j<getRows(); ++j){ cofactors = Cofactor(cofactors, 0, i, cols); det += sign * AccesValue(0,i) * cofactors.Determinant(); sign*=-1; } } return det; }
Am I doing something wrong ?
EDIT:
content of matrixcalculations.h:#ifndef MATRIXCALCULATIONS_H #define MATRIXCALCULATIONS_H #include <stdlib.h> #include <stdio.h> #define MAXSIZE 500 class Matrix{ public: Matrix(); Matrix(unsigned n, unsigned m); ~Matrix(); inline void SetMatrixDimension(unsigned n, unsigned m}{ rows = m; cols = n; } inline unsigned getRows() const { return rows; } inline unsigned getCols() const { return cols; } inline double AccesValue(unsigned n, unsigned m) const { return matrix[n][m]; } inline void setValue(unsigned n, unsigned m, double x) { matrix[n][m] = x; } Matrix Add(Matrix &A, Matrix &B) const; Matrix Subtract(Matrix &A, Matrix &B) const; Matrix Multiply(Matrix &B) const; Matrix Multiply(float c) const; / double Sum(Matrix &B, unsigned row, unsigned col) const; Matrix Inverse() const; Matrix Transpose() const; Matrix Cofactor(Matrix &B, unsigned x, unsigned y, unsigned n) const; double Determinant() const; private: unsigned rows; unsigned cols; double matrix[MAXSIZE][MAXSIZE]; }; #endif // MATRIXCALCULATIONS_H
-
Hi,
So you are in fact creating a table of 250'000 double on the stack each time you create a Matrix object.
I would recommend avoiding that as you are wasting quite a lot of resources for no benefits.
-
Hi
Well
you create it as
cofactors.SetMatrixDimension(N,N);
where N = cols. for both row and cols.then when you loop it
for(unsigned i=0; i<getCols(); ++i){
for(unsigned j=0; j<getRows(); ++j){and i wonder if getRows will be the same as N ?
-
@mrjj getCols() is just returning N, so
inline getCols() const { return cols; }
. So, the value is the same. And even if I comment out everything exceptMatrix Cofactors;
(and return some double) it is still crashing and giving the same error. -
@hra-ved
Ok. just checking. Then it's a bit odd. if only the declaration can crash.
And you call Determinant() for the same instance of Matrix as when it works
for Multiply()? ( so it's not an invalid instance)Also, the constructor of Matrix, is not calling Determinant() right?
(so we get infinite recursion) -
@mrjj It is called for the same instance for all functions, and constructor is not calling Determinant(). Also functions for transporting matrix is also crashing (also when declaring Matrix transponated;) , while functions for addiction, subtraction and matrix multiplication are working as intended,
-
Ok.
This custom Matrix-type you talk about.Matrix A;
seems very much like
Matrix cofactors;so they are the same type ?
what happens if you put
Matrix cofactors;
in
Matrix Matrix::Multiply(float c)
then it does work. ? -
@mrjj Now, if I rename Matrix A; to any other name (including cofactors), it is working and the same goes for Matrix cofactors;, if I rename it to Matrix A or Matrix somethingelse; it is crashing withe the same error. Also I have updated orig question with content of header.
Also I have noticed that if I change #define MAXSIZE 500 to 5, it is going to fall into loop. So that must be the issue ? But the same is not happening for some of the other functions .
-
Hi,
So you are in fact creating a table of 250'000 double on the stack each time you create a Matrix object.
I would recommend avoiding that as you are wasting quite a lot of resources for no benefits.
-
So it seems that I had two issues. Firstly, my program was creating table of 500*500 doubles every time I create a object of the Matrix class, and the second one was that I forgotten to set N-1 into determinat calculator. result was that my program was looping into infinity and every single time creating that massive matrix. Thank you very much for your help.