Long time VS 2013 Release Win 64 incorrect result

When I try to execute this code in VS 2013 / Win64 / Release, I get the wrong result: it prints 1 1.

In Visual Win32 / Debug / Release and Win64 / Debug, the result is correct.

A Visual Studio project was created with default settings.

#include <iostream>
int main(int argc, char* argv[])
{
   long long inc[2] = { 0, 1 };
   long long dinc[2] = { 0, 0 };

   dinc[0] = inc[1] - inc[0]; 
   dinc[1] = inc[0] - inc[1]; //expected -1 ==> display 1

   for (int i = 0; i < 2; ++i)
       std::cout << i << "\t" << dinc[i] << "\n";

   return 0;
}

How to explain this result?

+4
source share
1 answer

Turn off optimization for your project in Releaseforx64

right-click on Project-->Properties-->C/C++-->Optimization-->Disabled

Configurationin the upper left corner of the properties window should be set Release
Platformin the upper right corner should be setx64

EDIT: With a temporary variable in the middle, hold inc[0] - inc[1]

long long total = 0;
total   = inc[0] - inc[1];
dinc[1] = total;

the correct values ​​are printed using Optimization Enabled, regardless of its value

+1
source

All Articles