+ 5

How does the program output 26?

int m=2, n= 6; int &x=m; int &y=n; m=x++; x=m++; n=y++; y=n++; cout<<m<<n;

15th Jul 2018, 11:29 AM
silentlearner
silentlearner - avatar
8 odpowiedzi
+ 3
I'm short on time so here's what I see happening internally with an assembler (sorry, I did try to reasonably comment what I see happening) https://godbolt.org/g/XTBW4F Basically what's happening for e.g., m=x++ is: The assembly loads the variable x, then stores x+1 in a temporary register. Then the address of x is loaded again (into another place) and the +1 register value is saved back to x. Finally, the originally loaded value is saved back to m, overwriting the +1 that was just written to x. So...the +1 does happen, it's just overwritten, which (if I have this right) is happening because assignment and post-increment happen "at the same time" (sequence point) -- to the same memory location, and the compiler's saving the previous value -- so they clobber each other. Related...? If you substitute m=m++, the compiler actually generates the same move twice, which may be optimization of the sequence above.
25th Jul 2018, 3:53 AM
Kirk Schafer
Kirk Schafer - avatar
+ 7
It is an implementation-defined behavior. In GCC, the result is 2 and 6, but in VC++, the result is 4 and 8. I'm sure as hell that nobody bothers to use such catastrophic combination in practical programming. int m=2, n= 6; int &x=m; int &y=n; // In VC++ environment m=x++; // First m = x = 2, then x = 3, finally m = 3 x=m++; // First x = m = 3, then m = 4, finally x = 4 n=y++; // First n = y = 6, then y = 7, finally n = 7 y=n++; // First y = n = 7, then n = 8, finally y = 8 cout<<m<<n; // 48
15th Jul 2018, 1:53 PM
Babak
Babak - avatar
+ 6
Kirk Schafer C++ Soldier (Babak) How does the sequence point work in the program?
24th Jul 2018, 12:17 PM
silentlearner
silentlearner - avatar
+ 6
Thank you for your detailed explanation, Kirk Schafer.
26th Jul 2018, 1:14 AM
silentlearner
silentlearner - avatar
+ 5
Kirk Schafer +1 for "sequence point" or "sequencing" technicality. So, it's a UB then rather than a implementation-defined behaviour. " Other languages, such as C#, may define the precedence of the assignment and increment operator in such a way that the result of the expression i=i++ is guaranteed. " That explain why Visual C++ gives a more definitive result since it seems Microsoft's guaranteed such reliability across the whole family!
24th Jul 2018, 6:14 AM
Babak
Babak - avatar
+ 4
https://en.wikipedia.org/wiki/Sequence_point#Examples_of_ambiguity (starting with: "Sequence points also come into play when the same variable is modified more than once within a single expression.") Footnote [4] towards the end of this section reads (C99 specification): "Between the previous and next sequence point an object shall have its stored value modified AT MOST ONCE* by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." To see where sequence points occur, continue reading the next section "Sequence points in C and C++" * emphasis mine
24th Jul 2018, 12:30 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
silentlearner bro sorry for declining your challange request sololearn app leaks and shows error several times, then I declined your challange
26th Sep 2018, 6:38 AM
Sharofiddin
Sharofiddin - avatar
0
m and x are both pointing to same location. Same case for y and n; But in case of preincrement, the behaviour is different. What's happening????? And why?
15th Jul 2018, 1:20 PM
Dilip Srinivas
Dilip Srinivas - avatar