tags:

views:

114

answers:

2

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.

[edit] It's all completely my own fault. I stole some optimization and warning options from a question on here somewhere without really understanding them, and had since forgotten about them.

See Mark Rushakoff's answer, and in addition, I have also used -Wunsafe-loop-optimizations to explicitly warn if GCC is making assumptions about a loop. See http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

A: 

I think that GCC is telling you that it can't determine that the loop is not infinite and is carrying on with compilation regardless. It's a warning, not an error, something you may want to think about.

High Performance Mark
A run time error will never result from a compile time error, but may be a result of unheeded warnings.
James Morris
+8  A: 

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.
Mark Rushakoff
This seems to make sense to me.
James Morris