Vector of Class with Array
-
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
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 usingstd::vector
instead ofC
-like arrays. -
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 usingstd::vector
instead ofC
-like arrays.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?
-
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?
-
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?
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); -
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);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.
-
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.
-
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.
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. -
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.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
-
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
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 atBA
,F
and the result ofMA.inverse
at the time of the crash. -
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