FIX: Loop Optimization Causes Infinite Do-While Loop (115704)
The information in this article applies to:
- Microsoft C for MS-DOS 6.0
- Microsoft C for MS-DOS 6.0a
- Microsoft C for MS-DOS 6.0ax
- Microsoft C for OS/2 6.0
- Microsoft C for OS/2 6.0a
- Microsoft C/C++ for MS-DOS 7.0
- Microsoft Visual C++ 1.0
- Microsoft Visual C++ 1.5
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.
Modification Type: | Minor | Last Reviewed: | 7/5/2005 |
---|
Keywords: | kbbug kbCodeGen kbfix kbfix KB115704 |
---|
|