Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Vector of Class with Array

Vector of Class with Array

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structureshelp
11 Posts 3 Posters 0 Views 1 Watching
  • 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.
  • T T Bones Jones

    I have a class Matrix that I made years ago and has worked without problems. It looks like this:

    class Matrix
    {
    private:
    double *M;
    int ROWS, COLUMNS;
    public:
    Matrix (): M(new double[1]), ROWS(1), COLUMNS(1) { } // default constructor
    Matrix (int R, int C): M(new double[R*C]), ROWS(R), COLUMNS(C) { } // constructor
    ~Matrix() {delete[] M; }; // destructor
    Matrix(const Matrix& m): M(new double[m.ROWS*m.COLUMNS]), //copy constructor
    ROWS(m.ROWS), COLUMNS(m.COLUMNS)
    {for (int i=0; i<(ROWS*COLUMNS); i++) M[i]=m.M[i]; }

    double& operator() (const int R, const int C);                              //output a value from (row,col) cell
    double operator() (const int R, const int C) const;
    

    }

    In my new program, I have made use of a vector of this Matrix class, as in vector J; The problem comes when I add a new Matrix to this vector, as in: J.push_back(NewMatrix); The program will run fine 10 or 20 times, but then crash at this line with J.push_back(). I have tried using the vector like a conventional array, where I know the size, and use a for loop, in the form: for (unsigned i=0; i

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #2

    Quote:

    J.push_back(NewMatrix);

    This could be the problem. It is not clear how do you add Matrix instances to your vector. Could please post the actual code? By the way you could easily rewrite your Matrix class using std::vector instead of C-like arrays.

    In testa che avete, signor di Ceprano?

    T 1 Reply Last reply
    0
    • CPalliniC CPallini

      Quote:

      J.push_back(NewMatrix);

      This could be the problem. It is not clear how do you add Matrix instances to your vector. Could please post the actual code? By the way you could easily rewrite your Matrix class using std::vector instead of C-like arrays.

      T Offline
      T Offline
      T Bones Jones
      wrote on last edited by
      #3

      Here is the code section that actually crashes:

      for (unsigned i=0; imaxangle) maxangle=angle;
      }

      When it crashes it typically crashes at

      E.push_back(BA * F * MA.inverse());

      E is another vector of Matrix that was passed by reference to this function. My matrix class contains a number of functions that allows for matrix algebra. I know I could probably re-write the Matrix class to use vectors, but I've been using it for over 10 years (written before I knew about vectors) and I figured if it ain't broke don't fix it. But maybe it is broke, and it just took this long for it to become a problem. If it is broken, can you tell me where?

      L CPalliniC 2 Replies Last reply
      0
      • T T Bones Jones

        Here is the code section that actually crashes:

        for (unsigned i=0; imaxangle) maxangle=angle;
        }

        When it crashes it typically crashes at

        E.push_back(BA * F * MA.inverse());

        E is another vector of Matrix that was passed by reference to this function. My matrix class contains a number of functions that allows for matrix algebra. I know I could probably re-write the Matrix class to use vectors, but I've been using it for over 10 years (written before I knew about vectors) and I figured if it ain't broke don't fix it. But maybe it is broke, and it just took this long for it to become a problem. If it is broken, can you tell me where?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        T Bones Jones wrote:

        E.push_back(BA * F * MA.inverse());

        This is somewhat more complex than your original post suggested. You need to use the debugger to check the values of BA, F, MA, and whatever is returned by the call to MA.inverse().

        1 Reply Last reply
        0
        • T T Bones Jones

          Here is the code section that actually crashes:

          for (unsigned i=0; imaxangle) maxangle=angle;
          }

          When it crashes it typically crashes at

          E.push_back(BA * F * MA.inverse());

          E is another vector of Matrix that was passed by reference to this function. My matrix class contains a number of functions that allows for matrix algebra. I know I could probably re-write the Matrix class to use vectors, but I've been using it for over 10 years (written before I knew about vectors) and I figured if it ain't broke don't fix it. But maybe it is broke, and it just took this long for it to become a problem. If it is broken, can you tell me where?

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #5

          Richard is right, the expression in the push_back call is quite complex. You didn't show the inverse implementation and the operator * one. You know, in order to find the actual problem it could be useful to refactor a bit the code, e.g.:

          Matrix FMA = F * MA.inverse();
          Matrix BAFMA = BA * FMA;
          E.push_back(BAFMA);

          In testa che avete, signor di Ceprano?

          T 1 Reply Last reply
          0
          • CPalliniC CPallini

            Richard is right, the expression in the push_back call is quite complex. You didn't show the inverse implementation and the operator * one. You know, in order to find the actual problem it could be useful to refactor a bit the code, e.g.:

            Matrix FMA = F * MA.inverse();
            Matrix BAFMA = BA * FMA;
            E.push_back(BAFMA);

            T Offline
            T Offline
            T Bones Jones
            wrote on last edited by
            #6

            I did already do what you suggest, and replaced the matrix operation with a single Matrix value into the push_back. Since it didn't make a difference I did not include it here. However I am certain the the matrix operation I show does return a valid matrix -- ten year old code that has worked without issue before.

            CPalliniC L 2 Replies Last reply
            0
            • T T Bones Jones

              I did already do what you suggest, and replaced the matrix operation with a single Matrix value into the push_back. Since it didn't make a difference I did not include it here. However I am certain the the matrix operation I show does return a valid matrix -- ten year old code that has worked without issue before.

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #7

              Still, the vector push_back method is more tested. I see your code performs many operations. Could you reduce them ata the bare minimum still causing the error to occur? Then you could post such code in order to allow us to test it independently.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • T T Bones Jones

                I did already do what you suggest, and replaced the matrix operation with a single Matrix value into the push_back. Since it didn't make a difference I did not include it here. However I am certain the the matrix operation I show does return a valid matrix -- ten year old code that has worked without issue before.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                It is still not clear what error or exception causes the crash. Is it the data being passed to the call to push_back, or is the vector reference being corrupted in some way? You could try adding a try/catch block, or some logging messages around the point of the crash.

                T 1 Reply Last reply
                0
                • L Lost User

                  It is still not clear what error or exception causes the crash. Is it the data being passed to the call to push_back, or is the vector reference being corrupted in some way? You could try adding a try/catch block, or some logging messages around the point of the crash.

                  T Offline
                  T Offline
                  T Bones Jones
                  wrote on last edited by
                  #9

                  It is reasonable that I am being asked about my Matrix class and its functions. However, since I have been using it for over 10 years, I am fairly confident that its bugs have been worked out. The matrix multiplication and inversion functions produce the results that should be expected. So, I really do not think the problem is there, and asking you to examine that voluminous code would probably not be useful. If it helps, I tried the program written this way (differences on the last 7 lines):

                  for (unsigned i=0; imaxangle) maxangle=angle;
                  }

                  Again, the program can run several times without apparent error. When the error does occur it will even go several times through the for loop before it crash

                  L 2 Replies Last reply
                  0
                  • T T Bones Jones

                    It is reasonable that I am being asked about my Matrix class and its functions. However, since I have been using it for over 10 years, I am fairly confident that its bugs have been worked out. The matrix multiplication and inversion functions produce the results that should be expected. So, I really do not think the problem is there, and asking you to examine that voluminous code would probably not be useful. If it helps, I tried the program written this way (differences on the last 7 lines):

                    for (unsigned i=0; imaxangle) maxangle=angle;
                    }

                    Again, the program can run several times without apparent error. When the error does occur it will even go several times through the for loop before it crash

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    E.push_back(er); // when the program fails, this is where it happens

                    So that is where you need to look to try and establish why. Either E or er are not valid; but there is no way anyone here can guess which. Also, expressions like er=BA * F * MA.inverse(); do not make it easy. You need to look at BA, F and the result of MA.inverse at the time of the crash.

                    1 Reply Last reply
                    0
                    • T T Bones Jones

                      It is reasonable that I am being asked about my Matrix class and its functions. However, since I have been using it for over 10 years, I am fairly confident that its bugs have been worked out. The matrix multiplication and inversion functions produce the results that should be expected. So, I really do not think the problem is there, and asking you to examine that voluminous code would probably not be useful. If it helps, I tried the program written this way (differences on the last 7 lines):

                      for (unsigned i=0; imaxangle) maxangle=angle;
                      }

                      Again, the program can run several times without apparent error. When the error does occur it will even go several times through the for loop before it crash

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #11

                      This looks weird. I normally use min / max as a "limiter". Are the angles always valid?

                      if (anglemaxangle) maxangle=angle;

                      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups