Home Dashboard Directory Help
Search

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


Status: 

Closed
 as By Design Help for as By Design


1
0
Sign in
to vote
Type: Bug
ID: 773494
Opened: 12/4/2012 11:58:18 PM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

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);
}
//-----------------------------------------------------------------------------
Details
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)
Sign in to post a workaround.
File Name Submitted By Submitted On File Size  
PopFail.zip 12/5/2012 39 KB
PopFail.zip 12/5/2012 39 KB