Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program crashes with SIGSEGV error when declaring custom matrix type

Program crashes with SIGSEGV error when declaring custom matrix type

Scheduled Pinned Locked Moved Solved General and Desktop
qt6sigsegv
9 Posts 3 Posters 756 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hra-ved
    wrote on 23 May 2021, 08:43 last edited by hra-ved
    #1

    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
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 May 2021, 11:01 last edited by
      #8

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 23 May 2021, 08:54 last edited by
        #2

        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 ?

        H 1 Reply Last reply 23 May 2021, 08:58
        1
        • M mrjj
          23 May 2021, 08:54

          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 ?

          H Offline
          H Offline
          hra-ved
          wrote on 23 May 2021, 08:58 last edited by hra-ved
          #3

          @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 except Matrix Cofactors; (and return some double) it is still crashing and giving the same error.

          M 1 Reply Last reply 23 May 2021, 09:06
          0
          • H hra-ved
            23 May 2021, 08:58

            @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 except Matrix Cofactors; (and return some double) it is still crashing and giving the same error.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 23 May 2021, 09:06 last edited by
            #4

            @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)

            H 1 Reply Last reply 23 May 2021, 09:12
            1
            • M mrjj
              23 May 2021, 09:06

              @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)

              H Offline
              H Offline
              hra-ved
              wrote on 23 May 2021, 09:12 last edited by
              #5

              @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,

              M 1 Reply Last reply 23 May 2021, 09:32
              0
              • H hra-ved
                23 May 2021, 09:12

                @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,

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 23 May 2021, 09:32 last edited by
                #6

                @hra-ved

                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. ?

                H 1 Reply Last reply 23 May 2021, 10:17
                1
                • M mrjj
                  23 May 2021, 09:32

                  @hra-ved

                  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. ?

                  H Offline
                  H Offline
                  hra-ved
                  wrote on 23 May 2021, 10:17 last edited by hra-ved
                  #7

                  @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 .

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 23 May 2021, 11:01 last edited by
                    #8

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • H Offline
                      H Offline
                      hra-ved
                      wrote on 23 May 2021, 11:26 last edited by
                      #9

                      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.

                      1 Reply Last reply
                      1

                      2/9

                      23 May 2021, 08:54

                      topic:navigator.unread, 7
                      • Login

                      • Login or register to search.
                      2 out of 9
                      • First post
                        2/9
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved