FIX: Global Code Optimization Generates Incorrect Code

ID: Q115527


The information in this article applies to:


SYMPTOMS

The code below illustrates a bug in the C/C++ compiler that occurs when the /Ox, /Ol, /Oe, or /Og compiler switch is used. As a result of the optimization, the code below is stuck executing an infinite loop because of source code optimization. By using the /Fc compiler option, the code within the if statement is optimized out of the source code, as shown in the assembly output.


RESOLUTION

There are four workarounds to this problem:


STATUS

Microsoft has confirmed this to be a bug in the products listed above. This problem was corrected in C/C++ compiler version 9.0, included with Visual C++ 32-bit Edition, version 2.0.


MORE INFORMATION

The following sample demonstrates this problem.

Sample Code


/* Compile options needed: /c /Fc /Ox (or /Ol, /Oe, or /Og)
*/ 
      #include <stdio.h>
      static int fault, state;

      int test_func (void)
      {
         if (state == 2)
         {
            state=0;         // No code generated for this block if
      optimized
         /* state=0; */      // works if this line's comment is removed
            if (fault == 0)  // 
               return(1);      // 
            else             // 
               return(-1);     // 
         }
         else
            return(0);
      }

      void main(void)
      {
         state = 2;

         while (test_func() == 1);
         printf("done\n");
      } 

Additional query words: 1.00 1.50 7.00 8.00 8.00c


Keywords          : kb16bitonly kbCodeGen 
Version           : 7.00   | 1.00 1.50
Platform          : MS-DOS WINDOWS 
Issue type        : 

Last Reviewed: July 15, 1999