# A Funny C++ Bug

This topic is 3618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all: I wrote a piece of code for calculating the intersection between two planes. Here are my code:
bool ML_Plane::IntersectionPlane(ML_Plane& plane, ML_Line& line){
if ( Parallel(plane) ) return false;

double a1, b1, c1, d1;
double a2, b2, c2, d2;

a1 = GetNormal()[0]; b1 = GetNormal()[1]; c1 = GetNormal()[2];
d1 = GetNormal() * (*p0);

a2 = plane.GetNormal()[0]; b2 = plane.GetNormal()[1]; c2 = plane.GetNormal()[2];
d2 = plane.GetNormal() * plane[0];

double x=0;
double y=0;
if ( abs(a1*b2-a2*b1) > EPSILON){
x = (b1*d2-b2*d1)/(a1*b2-a2*b1);
y = (a2*d1-a1*d2)/(a1*b2*a2*b1);
}
//cout << "x=" << x << " y=" << y << endl;

ML_Vertex O(x,y,0);
ML_Vertex P = O + Cross(GetNormal(), plane.GetNormal() );

line = ML_Line(O,P);
return true;
}


Note that there is a line I comment out //cout << "x=" << x << " y=" << y << endl; Here is the driven code:
    ML_Vertex A(1,0,0), n1(0,1,0);
ML_Vertex n2(0,0,1);
ML_Plane p1(A, n1), p2(A, n2);

ML_Line line;
p1.IntersectionPlane(p2, line);
cout << line[0] << endl;
cout << line.GetNormal() << endl;


line[0] is a point on the line basically. This calculates the intersection between X plane and Y Plane. The problem is: when I comment the line " cout << "x=" << x << " y=" << y << endl;" out, the result is: 0 0 0 -4.94321e-37 -1 8.38816e-38 which is wrong. But if I leave the line uncommented, the result is : x=0 y=0 0 0 0 1 -1.52385e-314 0 which is correct, the intersection line normal direction is X axis. Can anyone tell me how to solve the problem please? My compiler is GNU C++ 4.1.2 Thanks

##### Share on other sites
Would you mind packaging the code you posted into a small complete program that has that weird behavior? It's hard to debug code that we can't run...

##### Share on other sites
Step through with your debugger and 'watch' those variables to find out when and where they seemingly change for no apparent reason.

Other than that you can try simplifying the code - like hard coding values that you know should produce a certain result - until the problem disappears, then you know that the last change you made must have been responsible.

Giving us a working, yet ideally minimalistic, example of your code could help us to diagnose it.

From the looks of your code you're maybe using pointers, perhaps you're relying on undefined behaviour somewhere by accessing invalid memory and this happens to be the way it's decided to kick you in the balls for it.

##### Share on other sites
    double x=0;  <-- A    double y=0;    if ( abs(a1*b2-a2*b1) > EPSILON){        x = (b1*d2-b2*d1)/(a1*b2-a2*b1);        y = (a2*d1-a1*d2)/(a1*b2*a2*b1);    }    //cout << "x=" << x << " y=" << y << endl;        ML_Vertex O(x,y,0); <-- B

There is no reason for x and y to actually exist in compiled code. The code path from A to B can store the values in registers, or in some other way. As such, the layout of variables on stack will vary.

If you do add cout statement, then x and y must exist, they need to be allocated, and almost certainly require stack space.

This type of problems is commonly related to stack or heap corruption. Why that happens however is impossible to tell from your code.

I do notice some unusual syntax around GetNormal(). What does that return?

##### Share on other sites
Quote:
 Original post by AsuralmHi all:I wrote a piece of code for calculating the intersection between two planes. Here are my code:*** Source Snippet Removed ***Note that there is a line I comment out //cout << "x=" << x << " y=" << y << endl;Here is the driven code:*** Source Snippet Removed ***line[0] is a point on the line basically. This calculates the intersection between X plane and Y Plane. The problem is:when I comment the line " cout << "x=" << x << " y=" << y << endl;" out, the result is:0 0 0-4.94321e-37 -1 8.38816e-38which is wrong.But if I leave the line uncommented, the result is :x=0 y=00 0 01 -1.52385e-314 0which is correct, the intersection line normal direction is X axis. Can anyone tell me how to solve the problem please?My compiler is GNU C++ 4.1.2Thanks

this isn't a bug with C++. it's a bug you've created.

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631362
• Total Posts
2999577
×