Microsoft KB Archive/254226

= BUG: Bad Code Generation with Global Optimization =

Article ID: 254226

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q254226



SYMPTOMS
The global optimizer may generate incorrect code if two if statements have identical conditional clauses that create side effects. Please refer to the sample code in the "More Information" section for details.



RESOLUTION
Following are two possible ways to work around this problem:


 * 1) Use the #pragma optimize("g",off) method to turn off global optimization at the function level. By making the function inline and compiling with the /Ob1 or /Ob2 compiler switches also alleviates this problem.
 * 2) Modify the code to avoid the construct in workaround number one. For example, when you change the second if statement to else if this change generates the correct code.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
The following sample code demonstrates the bug: //bug.cpp /* compiler option: cl /Og */
 * 1) include 

//#pragma optimize("g",off) // Uncomment this line to workaround 1. // inline void funcopt(int i) // Uncomment this line to workaround 2 and comment the function funcopt definition. void funcopt(int i) { if(i--==0) { printf("Zero\n"); }    if(i--==0) { printf("One\n"); }   return; }

int main {   funcopt(1); return 0; }

The expected output is 1.

Keywords: kbbug kbcodegen kbpending kbcompiler KB254226

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.