Infinite loop

Started by
13 comments, last by zoner7 14 years, 9 months ago
I've got some code that I recently added to my project. The program now stops responding after I start it, and I'm pretty positive that I created an infinite loop. Here is the code that I know is causing the issue; I'm just not sure how. the distance function computes the distance between two balls using the distance formula. .pos() recovers the position of an object. the position of a Ball is randomly defined in the constructor of Ball. Thank you. btw, if you know a less sloppy way to do this (I'm sure there is one, but my brain is foggy right now), I'd love to hear it.
[source lang=c++]
        BallHolder.reserve(15);
        //start with three balls to dodge

		BallHolder.push_back(Ball()); //we do not need to test position of the first ball... 
									  //additionally, this avoids address errors that would be caused by 
									  //a BallHolder.end() - 1).pos() test on an empty vector
		//ensure that balls do not spawn on eachother.
		vector <Ball>::iterator iter2;
		bool truth = false;
		while (BallHolder.size() < 4)
			truth = false;
			while (truth == false)
				truth = true;
				BallHolder.push_back(Ball());
				iter2 = BallHolder.end() - 1;
					for (vector <Ball>::iterator iter = BallHolder.begin(); iter < BallHolder.end() - 1; iter++) //ball should not test distance against itself
							if (  Distance( (*iter2).Pos(), playerBall.Pos()) <= 30  && Distance ( (*iter2).Pos(), (*iter).Pos() ) <= 30 )
								BallHolder.pop_back();
								truth = false;



Anyone have an idea what's wrong?
Advertisement
The problem is that C++ uses curly brackets to contain multiple statements, not indentation.

So the compiler is running this

while (BallHolder.size() < 4)    truth = false;


which is why you have an infinite loop.

You really want your code to be

while (BallHolder.size() < 4){    truth = false;		    while (truth == false)    {	truth = true;	BallHolder.push_back(Ball());	iter2 = BallHolder.end() - 1;	for (vector <Ball>::iterator iter = BallHolder.begin(); iter < BallHolder.end() - 1; iter++) //ball should not test distance against itself        {	    if (  Distance( (*iter2).Pos(), playerBall.Pos()) <= 30  && Distance ( (*iter2).Pos(), (*iter).Pos() ) <= 30 )            {						                BallHolder.pop_back();		truth = false;            }        }    }}
I thought curly brackets were not necessarily needed... I know that sometimes they can be omitted. When is that allowed?
If only one statement follows a loop or an if statement then you don't need the curly brackets. Otherwise you do.
Omitting curly brackets means only the next statement is executed inside the loop.

So

while (i < 100)    i++;


is the same as

while (i < 100){    i++;}


but

while (i < 100)  i++;  j++;


is the same as

while (i < 100){   i++;}j++;
I changed the code slightly... just added the brackets and changed an && statement to a || statement.

That was in the line

"if ( Distance( (*iter2).Pos(), playerBall.Pos()) <= 31 || Distance ( (*iter2).Pos(), (*iter).Pos() ) <= 31 )"

After doing this, I discovered a weird bug.

If the value in the

"while (BallHolder.size() < 8)"

statement is greater than 8, the program crashes when it starts, and I am told that vector iterator is not dereferencable. It occurs in the include/vector file...

I have absolutely no idea what may have caused this.
Quote:Original post by zoner7
If the value in the

"while (BallHolder.size() < 8)"

statement is greater than 8, the program crashes when it starts, and I am told that vector iterator is not dereferencable. It occurs in the include/vector file...

I have absolutely no idea what may have caused this.


Post that part of the code .... sounds like something after the end of the loop is farked.
Quote:Original post by HostileExpanse
Quote:Original post by zoner7
If the value in the

"while (BallHolder.size() < 8)"

statement is greater than 8, the program crashes when it starts, and I am told that vector iterator is not dereferencable. It occurs in the include/vector file...

I have absolutely no idea what may have caused this.


Post that part of the code .... sounds like something after the end of the loop is farked.


Here is the loop...

[source lang = c++]		BallHolder.push_back(Ball()); //we do not need to test position of the first ball... 									  //additionally, this avoids address errors that would be caused by 									  //a BallHolder.end() - 1).pos() test on an empty vector		//ensure that balls do not spawn on eachother.		vector <Ball>::iterator iter2;		bool truth = false;		while (BallHolder.size() < 10)		{			truth = false;			while (truth == false)			{				truth = true;				BallHolder.push_back(Ball());				iter2 = BallHolder.end() - 1;				for (vector <Ball>::iterator iter = BallHolder.begin(); iter < BallHolder.end() - 1; iter++) //ball should not test distance against itself				{					if (  Distance( (*iter2).Pos(), playerBall.Pos()) <= 31 || Distance ( (*iter2).Pos(), (*iter).Pos() ) <= 31 )					{						BallHolder.pop_back();						truth = false;					}				}			}		}


After the loop, the start function ends... Nothing after that was changed.

It is weird that the error doesn't occur when I make one slight change to the code.

If I change

[source lang=c++]if (  Distance( (*iter2).Pos(), playerBall.Pos()) <= 31 || Distance ( (*iter2).Pos(), (*iter).Pos() ) <= 31 )


to

[source lang=c++]if (  Distance( (*iter2).Pos(), playerBall.Pos()) <= 31 && Distance ( (*iter2).Pos(), (*iter).Pos() ) <= 31 )


everything works fine. All I did was change || to &&. Unfortunately, this is incorrect logic, and it does not do what I want it to do.

Alternately, I can just remove everything to the right of the || (including the ||, of course), and things work properly as well.

New Discovery: After coming back to my computer after a few hours, the program now runs when the value is 10, but it won't work with a higher value, such as 20.

EDIT: Could this be a problem with the address's that the (*iter) is accessing. If it is telling me the vector iterator is not dereferenceable, perhaps at a certain size something is happening so that iter is no longer pointing to an address in the vector BallHolder... This really doesn't make any sense seeing as this is such a small vector, and its position should not be changing... Not to mention, I thought you could deference any address, hence the cause of a stack overflow.

EDIT2: So the accepted values seem to vary... When using the || operator, on several runs in a row I was able to successfully run the game using 10; however, the program just crashed after while using that same value.
It looks like it's time to learn how to use your debugger. It will let you examine the program state as well as step through the execution so you can examine variables and see where it all goes wrong.
Not that I personally need one, but are there any down to earth n00bie friendly tutorials out there that go over how to use a debugger and go though some test cases of how to find bugs?

Just curious.
-=[Megahertz]=-

This topic is closed to new replies.

Advertisement