My environment is vs2013-update-5, I found my code has different result between debug and release, for example:
#include <iostream>
#include <string>
using namespace std;
int increase(int* x) {
int previous = *x;
*x += 1;
return previous;
}
int main() {
int data = 1;
int p = 2;
cout << ((p-data) + increase(&p)) << endl;
}
It prints 3 in debug mode and 4 in release mode. I know there's workaround like:
int x = p-data;
int y = increase(&p);
cout << (x + y) << endl;
Use some temporary variable to make sure the result. But in my real case, the code is used to parse some buffer to struct, there're lots of length checking like:
if((p-data) + 4 + forward<int>(&p) != data_len) return false;
// forward<unsigned>() increases p with sizeof(int), and return origin p
// just like the previous example
So it's best to write with one line code, not using temporary variables.
Why is this happen? Any compile flag causes this? How to make sure the code executed in order?