Article ID: 115704
Article Last Modified on 7/5/2005
APPLIES TO
- Microsoft C Professional Development System 6.0
- Microsoft C Professional Development System 6.0a
- Microsoft C Professional Development System 6.0a
- Microsoft C Professional Development System 6.0
- Microsoft C Professional Development System 6.0a
- Microsoft C/C++ Professional Development System 7.0
- Microsoft Visual C++ 1.0 Professional Edition
- Microsoft Visual C++ 1.5 Professional Edition
This article was previously published under Q115704
SYMPTOMS
The use of loop optimization (/Ol, or /Ox for the C/C++ compiler 8.0
for Windows NT) in a do-while loop that terminates after a single
iteration may cause an infinite loop. The code below can be used to
demonstrate this behavior. An infinite loop is generated when the
expression (i <= e) from the program below is true during the first
loop iteration.
CAUSE
Examining the assembly/source code file generated by using the /Fc
compiler option reveals that the comparison operation differs with
the optimized and non-optimized versions. The optimized version will
only reenter the loop if the two values are not equal, whereas the
non-optimized version correctly checks if i is less than or equal to
e.
Optimized version:
;|*** while (i<=e);
; Line 17
*** 000059 ff 4e f4 dec WORD PTR [bp-12]
*** 00005c 75 f1 jne $D536
Non-optimized version:
;|*** while (i<=e);
; Line 17
L00537:
*** 000054 8b 46 f6 mov ax,WORD PTR -10[bp]
*** 000057 39 46 fc cmp WORD PTR -4[bp],ax
*** 00005a 7f 03 e9 e7 ff jle L00536
RESOLUTION
There are two workarounds to this problem:
- Use the fast compiler option /f.
-or-
- Disable optimization during the function where the infinite loop occurs
by using the optimize pragma:
#pragma optimize("",off)
void bad_loop_function(void)
{
/* ... */
}
#pragma optimize("",on)
STATUS
Microsoft has confirmed this to be a bug in the products listed at the
beginning of this article. This problem was corrected in the C/C++ compiler
version 9.0, included with Visual C++ 32-bit Edition, version 2.0.
Additional query words: 6.00 6.00a 6.00ax 7.00 8.00 8.00c 1.00 1.50
Keywords: kbbug kbfix kbcodegen KB115704