Sign in to follow this  

Function parameters inadvertantly changing

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm passing an integer into a function by value. Going into the function, it's -1. However, after the first for loop within the function evaulates to false (the parameter is never touched within this loop), the integer magically changes into 1.2 million-something-or-other. I haven't touched the variable at all. If I make a copy of the number at the start of the function, both variables get screwed up. This is a bit of a burden, because I need that -1 later on in the function. What's going on here?

Share this post


Link to post
Share on other sites
Like I said, I'm not touching the integer at all. The parameter that's screwing up is ignoreIndex. It happens direct after the first for loop, before the following if statement evaluates.


bool TestPlacement(CS200::Polygon2D* tP, int ignoreIndex, CS200::Polygon2D* parent) {
// check for collisions
bool collision = false;
// circles test
for (int i=0; i<static_cast<int>(gCircleList.size()); i++) {
// ignore child objects during collision test
if (gCircleList[i]->parent != parent) {
collision = CollisionTest(tP, gCircleList[i]);
if (collision) break;
}
}
if (!collision) {
// polygon test
for (int i=0; i<static_cast<int>(gPolygonList.size()); i++) {
// ignore self and child objects during collision test
if (i != ignoreIndex && gPolygonList[i]->parent != parent) {
collision = CollisionTest(tP, gPolygonList[i]);
if (collision) break;
}
}
}
return collision;
}

Share this post


Link to post
Share on other sites
And as a sidenote, if I add this at the start of the function:


int test1 = ignoreIndex;
int test2 = ignoreIndex;


The debugger reports the following for each value at the end of the function:

Quote:

ignoreIndex: 4307031
test1: 4571380
test2: -858993460


Nice and random.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this