-1

According to the official C++ standard, could someone explain why there are differences in the output of the following simple code when run with different compilers?

In other words, does the standard leave this open as to what will happen first, the a++ or the b assignment?

This is not related to function parameters, it's the same piece of code run at different compilers. Here is the sample code:

#include <iostream>
using namespace std;

int main() {
    int a = 10, b;

    a = b = a+++a;
    cout << "a = " << a << ", b = " << b;

return 0;
}

Link of the code is here.

With g++ 4.7.2 and Intel C++ 13.0.1 you get:

stdout:
a = 21, b = 20

while with g++ 4.8.0 and Clang++ 3.2 you get:

stdout:
a = 21, b = 21

Which one is the right one? Thanks.

UpGM
  • 11
  • 2
  • 11
    Congratulations! You are the one millionth person to ask this question on stackoverflow :) ! – us2012 Feb 14 '13 at 13:22
  • @us2012 +1 for making me laugh out :) – Hayri Uğur Koltuk Feb 14 '13 at 13:25
  • 2
    All the compilers are right, but you are wrong for not turning on compiler warnings with `-Wall`, which would have saved you some time asking this question. Go to the back of the class – Jonathan Wakely Feb 14 '13 at 13:30
  • @us2012 Tell him what he's won, Jerry! "A braaannd new CAR!" – Collin Feb 14 '13 at 13:31
  • @us2012 dude, thanks for noting this. :) Now on to our topic: my question is, does the c++ standard leaves this to the compiler implementation to decide what's going to happen in such a case? Thanks – UpGM Feb 14 '13 at 13:37
  • While closing the question as exact duplicate is correct, I am not sure that the downvotes are so... the user might not know that this is undefined behavior (or even what undefined behavior means). It seems a bit harsh to downvote due to lack of knowledge... +1 to compensate part of it. – David Rodríguez - dribeas Feb 14 '13 at 14:07
  • @UpGM: Regarding your comment, the answer is yes or no, depending on how literal you take the question... undefined behavior means that the compiler can produce the same or different results in each run (or the compiler or theoretically even of the program). That is, if you recompile with the same compiler you can theoretically get a different result. The compiler implementation does not need to *decide* what it will do, that is what is called *unspecified behavior*, where the compiler vendor must take one of multiple options, *document it* and consistently produce the same result. – David Rodríguez - dribeas Feb 14 '13 at 14:11
  • 3
    @DavidRodríguez-dribeas Unspecified doesn't mean that the compiler implementor has to document it, or even be consistent; it just means that what may happen is limited. (Order of evaluation is unspecified, but there must be some order.) What you're thinking of is implementation defined. – James Kanze Feb 14 '13 at 14:52
  • @JamesKanze: Correct... I need to remember to first have coffee and only then open SO :) – David Rodríguez - dribeas Feb 14 '13 at 15:12

1 Answers1

1

All of the compilers are right.

In the line

a = b = a+++a;

you invoke Undefined Behaviour, because a gets modified twice without an intervening sequence point. If you do that, the C++ standard gives the compilers licence to do whatever they like.

Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
  • Ok, what is we had b = a+++a; Would it be the same case?.. – UpGM Feb 14 '13 at 13:40
  • @UpGM - Yes. There is absolutely no reason to write code like that. Just use `b = 2 * a; ++a;` or `b = 2 * a + 1; ++a;`, whichever result you want to have. – Bo Persson Feb 14 '13 at 13:47
  • @BoPersson I'm just asking in order to understand better the differences, not that I would write such code somewhere. Thanks! – UpGM Feb 14 '13 at 14:55