Microsoft's C++ compiler has violated the ISO language standard for at least the last three versions
Submitted for discussion... You will find this interesting. Take this program:
#include "stdio.h"
int main()
{
int a = 2;
int b = 3;
b++;
a = b++ * a++;
int c = a % b;
printf("a = %d b = %d c = %d",a,b,c);
return 0;
}
and compile it. Depending on where you compile, you get different results:
gcc: a = 8 b = 5 c = 3
MS c++ Version 6, 7, and 8: a = 9 b = 5 c = 4
MS c#: a = 8 b = 5 c = 3 So, I think this says that Microsoft's c++ compiler has violated the iso language standard at least for the last three versions, and they are now fixing the bug in their c# compiler. Think of all that MS code out there that is broken! |
|
|
Comments (5)
MS C++ is wrong.
a = b++ * a++;
is undefined behavior. Meaning that you can't portably rely on the result of this expression. Operator precedence has nothing to do with it; the C and C++ standards require that a variable can only be modified once within an expression. Attempts to modify it more than once are undefined.
See: http://www.eskimo.com/~scs/C-faq/q3.1.html http://www.eskimo.com/~scs/C-faq/q3.2.html http://www.eskimo.com/~scs/C-faq/q3.8.html
Remember, for your code to work `i = i++` must be valid, which would make `a[i] = i++` valid, and both of those ARE NOT VALID.
C# is completely different, as the order of evaluation within an expression is fully defined.
For example, even though there are 2 modifications to a, the comma operator makes the following expression valid and well-defined:
a = ++a, a + 1;
Chris
Is not defined by the C++ standard. You are invoking undefined behavior.
a = a++; alone is not defined.
In short the C++ language standard basically says that '=' is not a sequence point. All side effects must be complete before the next sequence point in the code but it doesn't specify when each side effect's evaluation has to happen during the evaluation of the previous expression.
As such the ONLY sequence point is the ';' and the incrementing of 'a' can happen before or after the assignment to 'a' .
assume the following:
a = 3;
a = a++;
Compiler 1 returns "4" for 'a'. Compiler 2 returns "3" for 'a'. Neither is incorrect but your code is not well formed.
Does that clear it up a bit? I'd steer clear of all postfix operators because people really don't know how to use them properly and their value is questionable to begin with.
Take the following:
std::vector::iterator it = string.begin();
for (; it != string.end(); it++) { //... code }
If you don't know why the above code is worse than:
for (l it != string.end(); ++it) { //... code }
then you probably shouldn't be using postfix ++ or --. Many C++ book authors choose to use i++ rather than ++i. I don't know why. I think it confuses people into thinking the wrong thing about for loops and side-effects in an expression are generally not well understood throughout the C++ and C communities.
Dave
Post a comment
A free account is needed to post. Write your comment here first: your text is kept in this browser while you sign up or log in, and is waiting for you when you come back.
Your message:Sign up and post (it is free) or Log in and post
