Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the explanation for "warning: assuming that the loop is not infinite"

Tags:

c

gcc

gcc-warning

I had just taken the decision to change as many variables from unsigned to int and upon recompiling the code in question, was greeted by this warning message:

freespace_state.c:203: warning: assuming that the loop is not infinite

The line in question:

for (x = startx; x <= endx; ++x, ++xptr)

This loop is 60 lines of code (inc white space/brackets etc), and has a goto within it, and at least one occurrence of continue.

In this case, I think I am appreciative that GCC is assuming this loop is not infinite, because, it should never loop indefinitely.

What is GCC trying to tell me here?

The grammar of the warning is almost suggestive that the warning should be taken within the context of some other warning, but there are none within that context.

like image 212
James Morris Avatar asked Jun 05 '10 23:06

James Morris


People also ask

What causes an infinite loop error in a while statement?

Infinite loops This happens when the true/false expression never returns a false value. While there can be some cases where you might want this to happen, most often an infinite loop is the result of a bug in your program's logic.

What causes a loop to be infinite?

Usually, an infinite loop results from a programming error - for example, where the conditions for exit are incorrectly written. Intentional uses for infinite loops include programs that are supposed to run continuously, such as product demo s or in programming for embedded system s.

What is an infinite loop explain with an example?

An infinite loop occurs when a condition always evaluates to true and because of this the loop control doesn't go outside of that loop. Example: i = -1. while(i != 0): print(1)

What happens if you run an infinite loop?

What are infinite loops? An infinite loop is a piece of code that keeps running forever as the terminating condition is never reached. An infinite loop can crash your program or browser and freeze your computer.


1 Answers

According to this GCC patch from 2005, it appears that GCC is performing an "unsafe loop optimization" (and you are being warned because -funsafe-loop-optimizations is set). If the loop is infinite, this particular optimization will fail somehow.

Since you said it is a terminating loop, it sounds as though you have nothing to worry about.

Another relevant part of the patch:

@opindex Wunsafe-loop-optimizations
Warn if the loop cannot be optimized because the compiler could not
assume anything on the bounds of the loop indices.  With
@option{-funsafe-loop-optimizations} warn if the compiler made
+such assumptions.
like image 67
Mark Rushakoff Avatar answered Sep 25 '22 03:09

Mark Rushakoff