|
leon de boer wrote: Even stopped it optimizing away but atill doesn't return the int it declares it will.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Hey it was late only a cup of coffee between good code and bad code !!!
You are right I say return 0 to you
In vino veritas
|
|
|
|
|
As k5054 says, the #include is a preprocessor directive. But, even worse, it contains a plethora of other directives.
|
|
|
|
|
How to use the Google drive, One drive and Dropbox REST api in my console program to download a file from my dropbox shared link
|
|
|
|
|
Those are 3 white papers, not "questions".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
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) { }
Matrix (int R, int C): M(new double[R*C]), ROWS(R), COLUMNS(C) { }
~Matrix() {delete[] M; };
Matrix(const Matrix& m): M(new double[m.ROWS*m.COLUMNS]),
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);
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<matrix> 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 using std::vector instead of C -like arrays.
|
|
|
|
|
 Here is the code section that actually crashes:
for (unsigned i=0; i<frames; i++)
{
Matrix F(4,4);
double angle;
F=BA.inverse() * Jtertiary.at(i) * MA;
trans(x)=F(x,3); trans(y)=F(y,3); trans(z)=F(z,3);
trans(tertiaryaxis)=0;
if (tertiaryaxis==X)
{
angle=ExtractX(F.inner(), RotationSequence);
F=(Xrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=0; F(y,3)=trans(y); F(z,3)=trans(z);
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else if (tertiaryaxis==Y)
{
angle=ExtractY(F.inner(), RotationSequence);
F=(Yrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=trans(x); F(y,3)=0; F(z,3)=trans(z);
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else if (tertiaryaxis==Z)
{
angle=ExtractY(F.inner(), RotationSequence);
F=(Zrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=trans(x); F(y,3)=trans(y); F(z,3)=0;
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else
throw("ERROR: Tertiary Axis designation invalid for Three Axis Functional Alignment.\n");
F3.at(i)(0)=degrees(angle);
E.push_back(BA * F * MA.inverse());
if (angle<minangle) minangle=angle;
if (angle>maxangle) 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?
|
|
|
|
|
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() .
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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; i<frames; i++)
{
Matrix F(4,4);
double angle;
F=BA.inverse() * Jtertiary.at(i) * MA;
trans(x)=F(x,3); trans(y)=F(y,3); trans(z)=F(z,3);
trans(tertiaryaxis)=0;
if (tertiaryaxis==X)
{
angle=ExtractX(F.inner(), RotationSequence);
F=(Xrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=0; F(y,3)=trans(y); F(z,3)=trans(z);
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else if (tertiaryaxis==Y)
{
angle=ExtractY(F.inner(), RotationSequence);
F=(Yrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=trans(x); F(y,3)=0; F(z,3)=trans(z);
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else if (tertiaryaxis==Z)
{
angle=ExtractY(F.inner(), RotationSequence);
F=(Zrot(angle).transpose() * F.inner()).homogeneous();
F(x,3)=trans(x); F(y,3)=trans(y); F(z,3)=0;
F3.at(i)(1)=trans(x); F3.at(i)(2)=trans(y); F3.at(i)(3)=trans(z);
}
else
throw("ERROR: Tertiary Axis designation invalid for Three Axis Functional Alignment.\n");
F3.at(i)(0)=degrees(angle);
Matrix er;
er=BA * F * MA.inverse();
cout << i << endl;
er.dump();
E.push_back(er);
if (angle<minangle) minangle=angle;
if (angle>maxangle) 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 crashes. In the format I am giving here, it will print out the iteration of the loop and the matrix that was provided to the E vector (which should be pretty close to a 4x4 identity matrix).
The program just stops. No error messages are given. I've tried throw-catch around it, but I don't know what to catch here other than ..., and that doesn't work.
In my experience, odd behavior like this is due to a memory leak. But, the only place I can think of where memory is leaking would be if the Matrix destructor is not being called when the E (or other similar vectors like F3) goes out of scope.
Am I correct in this suspicion? If so, how do I invoke an array destructor before the vector of arrays goes out of scope?
I know that many of you want me to re-write the Matrix class with vectors instead of dynamic arrays. That may be the only correct ultimate solution to my problem. But right now, this is also an academic question about the proper use of a vector of arrays.
And, of course, if the vector of arrays issue is not my problem, then I am really lost and in greater need of any help that can be offered.
Thanks for the efforts so far, and for any efforts that appear in the future to help me solve this thing.
|
|
|
|
|
E.push_back(er);
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.
|
|
|
|
|
This looks weird. I normally use min / max as a "limiter". Are the angles always valid?
if (angle<minangle) minangle=angle;
if (angle>maxangle) maxangle=angle;
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Does C implement the concept of a namespace?
modified 17-Apr-19 5:39am.
|
|
|
|
|
|
|
C does not provide support for namespace like C++. Without Namespace, we cannot declare two variables of the same name.
We can count Namespaces as a
limitation of C programming Langauge.
|
|
|
|
|
You mean you can not have the same name in the same scope.
It is routine to carry the same name such as i, j, count etc but they get restricted to
within a local scope as a local variable in a function or unit.
Not like there should be many but global variables obviously can't have the same name.
In vino veritas
|
|
|
|
|
It is not a limitation. When C was designed there was not thought to be a need for such a feature. But you could still have variables and functions that were limited to specific source modules. And local variables inside functions were hidden from others automatically.
|
|
|
|
|
Just to be clear namespaces should not be considered a limitation or a feature that is used to protect the scope of variables.
Rather it should be considered as a way to protect other entities like classes and methods.
If you have a problem with namespace collision in scopes with variables then you need to refactor your code so variables are not exposed at all.
|
|
|
|
|
Why C is not popular among developers?
|
|
|
|
|
|