Microsoft KB Archive/115527

{|
 * width="100%"|

FIX: Global Code Optimization Generates Incorrect Code

 * }

Q115527

-

The information in this article applies to:


 * The C/C++ Compiler (CL.EXE), included with:
 * Microsoft C/C++ for MS-DOS, version 7.0
 * Microsoft Visual C++, versions 1.0, 1.5

-

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:

 Use the fast compiler. You can force the compiler to use this option by adding the /f option. Compile without the /Ox, /Ol, /Oe, or /Og options.  Disable optimization for the function generating the incorrect code. This is done by using the optimize pragma as follows:      #pragma optimize("",off)

int test_func(void) {      /* .... */       }

#pragma optimize("",on)   In the case of the code shown below in the "MORE INFORMATION section, repeat the line:      state=0;  to cause the compiler to generate the correct assembly code. 

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      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

Issue type :

Technology : kbVCsearch kbAudDeveloper kbCVCComp