Microsoft KB Archive/281424

= FIX: Bad Code Is Generated with /Og Enabled =

Article ID: 281424

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual C++, 32-bit Enterprise Edition 6.0 SP4
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++, 32-bit Learning Edition 6.0 SP4

-



This article was previously published under Q281424



SYMPTOMS
With the global optimizations (/Og) compiler option enabled, the Visual C++ 6.0 Service Pack 4 (SP4) compiler may generate incorrect code for return values to functions. See the &quot;More Information&quot; section for a detailed reproducible sample.



CAUSE
This is a regression bug that occurred in Visual Studio Service Pack 4.



RESOLUTION
To work around this bug, do one of the following:   Replace the following line in the sample code Test (MyClass::Testfunc, y, z); with the following: MyClass x = MyClass::Testfunc; Test (x, y, z); -or-  In the sample code, use #pragma optimize(&quot;g&quot;, off) before the main function to disable the optimizations only in main.



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

This problem was corrected in Visual Studio 6.0 ServicePack 5.



Steps to Reproduce Behavior
// Test.cpp // Command line : cl test.cpp /Og


 * 1) include 

class MyClass { public: unsigned long mID; MyClass(unsigned long id); MyClass(const MyClass & other); MyClass & operator = (const MyClass & other) ; unsigned long getID {      return mID; }  static MyClass Testfunc {      return 0; }; };

MyClass::MyClass (unsigned long id /* = 0 */) {  mID = id; }

MyClass::MyClass(const MyClass & other) : mID(other.mID) {  cout << &quot;In Copy Constructor&quot; << endl; }

MyClass & MyClass::operator = (const MyClass & other) {   cout << &quot;In Operator =&quot; << endl; mID = other.mID; return *this; }

void Test (MyClass x, int y, int z) { cout << &quot;In Function Test&quot; << endl; cout << x.getID << ' ' << y << ' ' << z << endl; }

int main {  int y = 1; int z = 2; Test (MyClass::Testfunc, y, z); // expected output is 0 1 2 return 0; } The following is the expected output:

In Function Test

0 1 2

Actual Output:

In Function Test

1245052 0 2

Additional query words: kbbug kbfix

Keywords: kbbug kbfix kbvc600fix kbcompiler KB281424

-

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

© Microsoft Corporation. All rights reserved.