BUG: Optimization Generates Incorrect Results for an Inline Function Returning double (243534)



The information in this article applies to:

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

This article was previously published under Q243534

SYMPTOMS

When a method of one class calls, as an input argument, an inline function that creates an object of another class and returns a double, the compiler may generate incorrect results.

CAUSE

This occurs when compiled with the /Ob1 (Only _inline) or /Ob2 (Any Suitable) optimizations turned on.

RESOLUTION

There are two possible ways to work around this problem:
  • Compile the project with the /Ob1 (Only _inline) or /Ob2 (Any Suitable) optimizations turned off.
  • Store the results of the function that is used as an input parameter in a temporary variable. Pass the temporary variable as the argument to the method of the class that returns a double.

STATUS

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

MORE INFORMATION

The following code demonstrates the problem and the second workaround:
// test.cpp
// cl /GX /O2 /Ob1 test.cpp
#include <iostream.h>

class secClass
{
public:
	secClass(const char *) {};
	~secClass() {};
private:
};

class firstClass
{
public:
	firstClass() {}
	void setNum(double num) { cout << "The number is " << num << endl; };
};


double getNum(secClass str)
{
	return 7.0;
}

int main(int argc, char* argv[])
{
	firstClass f;
        cout << "Incorrect Results" << endl;
	f.setNum(getNum("Test"));
	//Second workaround.
        cout << "Correct Results" << endl;
        double d = getNum("Test");
	f.setNum(d);
        return 0;
}
				
The output of the above program is as follows:
Incorrect Results
The number is 1.84959e-307
Correct Results
The number is 7
				

Modification Type:MajorLast Reviewed:11/18/2003
Keywords:kbBug kbCodeGen kbCompiler kbCPPonly kbnofix KB243534