Microsoft KB Archive/135526

From BetaArchive Wiki

Article ID: 135526

Article Last Modified on 12/8/2003



APPLIES TO

  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 1.5 Professional Edition
  • Microsoft Visual C++ 1.51
  • Microsoft Visual C++ 1.52 Professional Edition
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 2.2
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 4.1 Subscription
  • Microsoft Visual C++ 4.2 Enterprise Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 4.2 Professional Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q135526

SYMPTOMS

Invalid results may be generated under these conditions:

  • Your code defines an inline function that uses global or static data, and it invokes that function twice within an expression.
  • You are using the /Ob1 or /Ob2 or /Ox compiler optimization switch with the 32-bit compiler, or you are using the /Ob1g or /Ob2g or /Ox switch with the 16-bit compiler.
  • The global or static variable appears in the return expression for the function.


RESOLUTION

Do not use the above optimizations.

-or-

Assign the return value of one of the function calls to a local variable. Then use the local variable in the expression.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

This problem was corrected in Microsoft Visual C++ .NET.


MORE INFORMATION

Sample Code to Reproduce the Problem

/* Compile options needed 32-bit Compiler: /Ob1, or /Ob2, or /Ox
/* Compile options needed 16-bit Compiler: /Ob1g, or /Ob2g, or /Ox
*/ 

#include <stdio.h>

int  nGlobal = 0;

inline int test(int nIn) {
   return  nGlobal = nIn;
}

void main() {
   // This will incorrectly evaluate to true.
   if  (test(1) == test(2))
       printf("test(1) == test(2)\n");
   else
       printf("test(1) != test(2)\n");

   // This will correctly evaluate to false.
   int n = test(2);

   if  (test(1) == n)
       printf("test(1) == test(2)\n");
   else
       printf("test(1) != test(2)\n");
}
                


Additional query words: kbVC400bug 8.00c 9.00 9.10 10.00 10.10 10.20

Keywords: kbbug kbfix kbnoupdate kbcodegen kbcpponly kbcompiler KB135526