KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q66216: Loop Optimizations May Generate Bad Code on Nested Loops

Article: Q66216
Product(s): See article
Version(s): 6.00   | 6.00
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | buglist6.00 buglist6.00a | mspl13_c
Last Modified: 24-OCT-1990

The code below illustrates a problem in using loop optimizations with
nested loops.

With loop optimizations disabled, the compiler calculates the value of
x for each iteration of the loop and produces the correct result.

When loop optimizations are turned on, the compiler initializes the
value of x and subtracts it by 10 on each loop iteration. The problem
occurs here with the miscalculation of x when variable i increments.

Workaround
----------

The only workaround is to compile without loop optimizations, which
can be done in the following ways:

1. Do not use the /Ol option.

2. Insert the #pragma optimize statement in the code to turn off loop
   optimization for the particular function.

Microsoft has confirmed this to be a problem in C version 6.00 and
6.00a. We are researching this problem and will post new information
here as it becomes available.

Sample Code
-----------

#include<stdio.h>

void main(void)
{
     int i, j, x;

     for (i=0; i<2; i++)
     {
          for (j=0; j<4; j++)
          {
               x=(i*4+3-j)*10;
               printf("%d\n", x);
          }
     }
}

The following table demonstrates the output when the code above is
compiled with and without loop optimizations:

   Correct Output Without /Ol     Incorrect Output with /Ol
   --------------------------     -------------------------

      30                             30
      20                             20
      10                             10
       0                              0
      70                             -10
      60                             -20
      50                             -30
      40                             -40

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.