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.
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.
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.
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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With