Different Results in Debug- and Release-Compilations - Optimization problem ? - by Andrew G Gray

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 773494 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 12/4/2012 11:58:18 PM
Access Restriction Public

Description

When I compile the following code in Visual Studio 11.0.51106.01-Update 1, I get
diffrent results between debug- and release-compilations.

//-----------------------------------------------------------------------------
ref class A
{
  public:
  A()
  {
    m_stack = gcnew array<double>(32);
    m_sp = 0;
  }

  double pop()
  {
    return m_stack[--m_sp];
  }

  void push(double val)
  {
    m_stack[m_sp++] = val;
  }

  void sub()
  {
    push(-pop() + pop());  // !!!!! Behaves different in Debug- and Release-Compilations
  }

private:
  array<double>^  m_stack;
  int                      m_sp;
};


int main()
{
  A^ a = gcnew A;

  a->push(6);
  a->push(2);
  a->sub();

  // in Debug-Version the correct result of 4 is produced
  // in Release-Version the result  is -4 which is fail
  double res = a->pop();

  System::Console::WriteLine("6 - 2 = {0}", res);
  System::Console::ReadKey(true);
}
//-----------------------------------------------------------------------------
Sign in to post a comment.
Posted by Microsoft on 1/17/2013 at 8:04 PM
Hi, the behavior is by design.
The code should not rely on evaluation order like this.
If you split the expression, the program will return correct results for all optimizations:
        double op1 = -pop();
        double op2 = pop();
        push(op1 + op2);
Let us know if you have any other questions or concerns.
Thank you,
Andreea Isac
Visual C++ Compiler Backend Team
Posted by Microsoft on 12/5/2012 at 2:07 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 12/5/2012 at 12:51 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)