What is the reason for running ++X an unexpected way? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is the reason for running ++X an unexpected way?

Why does any of these options work here in Sololearn C ++ - but it doesn’t work for example in Visual Studio 15 C ++ and not only ... ??? https://code.sololearn.com/cuFUVy3QdHFR/#cpp Result from Visual Studio 2015 (not true of course): 7 -> 7 -> 7 -> 7 -> 7 -> 7 7 -> 7 -> 7 -> 7 -> 7 -> 7 7 -> 6 -> 7 -> 6 -> 7 -> 6 2 -> 1 -> 1 -> 1 -> 1 -> 1 2 -> 3 -> 4 -> 5 -> 6 -> 7 Result from SoloLearn SandBox (true): 2 -> 3 -> 4 -> 5 -> 6 -> 7 2 -> 3 -> 4 -> 5 -> 6 -> 7 2 -> 3 -> 4 -> 5 -> 6 -> 7 2 -> 3 -> 4 -> 5 -> 6 -> 7 2 -> 3 -> 4 -> 5 -> 6 -> 7 optimization ? C++ compiler version ? Statdart C++ ? ???

24th Nov 2019, 6:35 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
7 Answers
+ 2
Undefined behavior. "Modifying an object between two sequence points more than once produces undefined behavior". https://en.wikipedia.org/wiki/Undefined_behavior https://www.geeksforgeeks.org/undefined-behavior-c-cpp/
24th Nov 2019, 6:54 PM
Diego
Diego - avatar
+ 3
There is no problem in fact. It just got interesting - Thanks.
24th Nov 2019, 11:20 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 2
Thanks. I knew about the incorrectness of attempts to do this ... it was interesting here: is it possible to somehow stabilize it, for example, with the compilation option ... But such an idea arose when I saw that this particular SoloLearn example of sandboxes works stably.
24th Nov 2019, 7:07 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 2
So no one answered - why does the sandbox always work in SoloLearn like it does not work anywhere? Perhaps this is a question for the developers of the C ++ environment for SoloLearn?
27th Nov 2019, 8:25 AM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 1
https://en.cppreference.com/w/cpp/language/eval_order "Order of evaluation of any part of any expression, including order of evaluation of function arguments is unspecified. The compiler can evaluate operands and other subexpressions in any order, and may choose another order when the same expression is evaluated again."
24th Nov 2019, 6:55 PM
Dennis
Dennis - avatar
+ 1
Wasn't the definition of UB even stricter? Like: Modifying an object between two sequence points only once but using it a second time can already lead to issues?
24th Nov 2019, 10:19 PM
HonFu
HonFu - avatar
+ 1
Well, it does show the same output as on sololearn in visual studio 2019 with the c++17 or c++2a flag set. But gcc has always been the odd one out with all the c++ extensions to be honest. :)
27th Nov 2019, 8:49 AM
Dennis
Dennis - avatar