Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stuck in infinite loop 2

Status
Not open for further replies.

Mungovan

Programmer
Oct 24, 2002
94
0
0
IE
Hi, I have this code in a program that seems to get caught in this infinite loop and won't get out of it:


>>>>>>>>>>>>>>>>>>>>>>

else{
for(i=0; i<size; i++){
if(condition==FALSE && x!=0{
addToArray(a, i);
}
}
}

>>>>>>>>>>>>>>>>>>>>>>>>>>

Basically the values are as follows:
size >= 1 (but less than 10)
condition is a boolean TRUE or FALSE
x an integer value
addToArray - adds i to the array a.

Why is it getting stuck in this loop!!!!!
Please!

Thanks,
D

 
It would seem the value in variable 'i' is always less than the value in variable 'size'. This could happen if 'size' contains a value that 'i' could not possibly represent, for example:

unsigned char i;
short size = 500;

for ( i=0; i<size; i++ ) {
/* infinite loop */
}

In the example, 'i' cannot represent the value in size, since 255 is the maximum value an unsigned char can hold (assuming a char is one byte on your system).

Make sure 'i' and 'size' are of the same data type. Then make sure (possibly by using a debugger) that 'size' holds the value you think it does. If you do not have access to a debugger, insert a printf() statement before your for loop that shows the value of 'size':

printf( &quot;Size is: %d\n&quot;, size );


If this does not solve your problem, please post more of your code.

Good luck,
Jason
 
Are the loops in your addToArray function straight? If the data types of i and SIZE are not the same like Jason said, then thats the only other thing that may be the problem. Unless, of course the infinite loop is in another part of your code.

Peace out.
 
I'm very curious to your implementation of addtoarray.

I am suspecting that you are overwriting a part of your stackframe, possibly resetting either i or size or both.

Tip: try to avoid testing against FALSE or TRUE: firstly, this may lead to code that looks OK, but behaves bad. Secondly, if() is supposed to evaluate using boolean values. Instead of writing if(value == FALSE), you can also use if(!value). Instead of writing if(value == TRUE), you can also write if(value).

Try to think what happens if value!=TRUE and value!=FALSE (e.g. value = 2), it will take the (logically) wrong route if tested against TRUE.

HTH
Pieter
 
The loop might not be in the main function but it could be in the procedure AddToArray(a,i) your calling. How does this procedure look like ?
 
Here I have a doubt what really is happening in the function
addToArray is it just adding i to the array a or is it more.
And r u varying the value of size in the function????
Do let me know.
if(condition==FALSE && x!=0{
Should this line be....?????
if(condition==FALSE && x!=0){

BYE,
Qsudhi
 
In function addToArray check that your not going past the array boundary because if you are its possible that your are writing in memory that was allocated to another variable, in this case maybe the var i is over written.

Jim ----------------------------------------
Friends are generally of the same sex, for when men and women agree,it is only in the conclusions; their reasons are always different.
 
Isn't it very easy to test: just comment out the AddToArray and see if there is still an infinite loop. If yes, then there is a problem with i. If not, then there is a problem in AddToArray.
I assume i isn't global.
 
Hey!
Thanks for everyone's help. Turns out that the problem is the i was being used for a larger loop that this new small loop was a part of. Now I am using a different variable instead of i.

Thanks everyone!!!

D

 
your loop should have terminated regardless of whether
the i variable was part of a larger loop. since the I variable is initialized as 0 and the loop is self contained
i.e. does not appear to rely upon ouside variable to increment the i. I say does not appear because I suspect that you are passing the i varible to the addtoarray function by reference and this function is adversely chnging the varible to disallow a loop termination. at the next iteration of the loop the i variable is not < size by the action of the addtoarray function.

try this

else
{
int y;
for(i = 0; i < size; i++)
{
if(condition == FALSE && x != 0)
{
y = i;
addToArray(a, y);
}
}
}

unless size is very large as to appear to be infinite
upon the run of the code.

tomcruz.net
 
That's why I asked about i being global. This is one of the dangers of the scopes of variables: it's a good idea to make sure your variables have as small a scope as possible, because that reduces the risk of changing them somewhere else. If AddToArray also uses i then it's highly likely everything will go pear-shaped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top