![]() |
|
Logic Unknown - help needed - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: Logic Unknown - help needed (/Thread-Logic-Unknown-help-needed) |
RE: Logic Unknown - help needed - Deque - 04-19-2013 (04-18-2013, 11:10 PM)ArkPhaze Wrote: Therefore @Deque you are also wrong. :lol: So you just take as a given that the side effect of y++ takes place before the ++y is evaluated? You can't. For the compiler it would be right to do this: y=3 y++ --> value of y is still 3, increment happens later ++y --> value of y is 4 x = 4 + 3 --> = 7 increment y --> value of y is 5 So, again, the behaviour is undefined, no matter how you put it. RE: Logic Unknown - help needed - Deque - 04-19-2013 (04-18-2013, 11:10 PM)ArkPhaze Wrote: Therefore @Deque you are also wrong. :lol: So you just take as a given that the side effect of y++ takes place before the ++y is evaluated? You can't. For the compiler it would be right to do this: y=3 y++ --> value of y is still 3, increment happens later ++y --> value of y is 4 x = 4 + 3 --> = 7 increment y --> value of y is 5 So, again, the behaviour is undefined, no matter how you put it. RE: Logic Unknown - help needed - ArkPhaze - 04-19-2013 (04-19-2013, 06:00 AM)Deque Wrote: So you just take as a given that the side effect of y++ takes place before the ++y is evaluated? You can't. Yes, in this case you can't, and so I never said it was that way. Read my edit. I only mentioned that it didn't matter which way things are evaluated, for the reasons I have shown in the later part of my post. ++y and then y++, or the other way around, you'll still end up with 8. And you are still wrong lol. I don't care what kind of compiler you have, there's no way that this can be 7. :lol: Quote:y++ --> value of y is still 3, increment happens later Yes it happens later, but immediately after, so immediately after that, y = 4, NOT 3. And when ++y gets evaluated, it takes that 4, and increments it by 1 giving you an immediate 5, and NOT the 4 that you think it is. After y++ is evalutated in that expression within the line itself, the value is seen as 3, but is incremented to 4 after, in which gets further incremented to 5 by ++y. You're both getting this wrong here... This is the way auto-incrementing operators actually work: Code: y=3
y++ --> value of y is still 3, increment happens later
{y incremented here: y is now 4}
++y --> value of y is 4 {auto-incremented to 5 during this evaluation}
x = 3 + 5 --> = 8I'm confused on why you both think it is (or should be) 7... As you can see in my example below, the incrementing is during evaluation. Whether we use the incremented value or the original before it is incremented is also determined. Code: int x = 5;
printf("x++: (Starting with x = %d)\n", x);
printf("\tEvaluation Time: %d\n", x++);
// x is incremented after the expression
printf("\tAfter %d\n", x);
x = 5;
// x is incremented during the expression
printf("\n++x: (Starting with x = %d)\n", x);
printf("\tEvaluation Time: %d\n", ++x);
printf("\tAfter %d\n", x);![]() Read my post again where I explain the math behind each way the compiler can interpret it: Quote:[y = 3] for ... x=(y++)+(++y): These are the same: Code: int x = 5;
printf("%d\n", ++x);
x = 5;
printf("%d\n", x=x+1);They both do this: Code: mov eax,dword ptr [x]
add eax,1
mov dword ptr [x],eax1. So at the time variable++ happens, the value of variable immediately after this, is variable + 1. 2. The variable which was incremented is then incremented and the incremented value is taken immediately as ++x, or x+=1/x=x+1 as shown above. This means that if variable was 3, the variable value after variable++ is 4. At the time of ++variable, that 4 is taken to 5, and that 5 is used in the expression. Thus... value = 3 + 5, not 3 + 4. I can prove it if you want. RE: Logic Unknown - help needed - ArkPhaze - 04-19-2013 (04-19-2013, 06:00 AM)Deque Wrote: So you just take as a given that the side effect of y++ takes place before the ++y is evaluated? You can't. Yes, in this case you can't, and so I never said it was that way. Read my edit. I only mentioned that it didn't matter which way things are evaluated, for the reasons I have shown in the later part of my post. ++y and then y++, or the other way around, you'll still end up with 8. And you are still wrong lol. I don't care what kind of compiler you have, there's no way that this can be 7. :lol: Quote:y++ --> value of y is still 3, increment happens later Yes it happens later, but immediately after, so immediately after that, y = 4, NOT 3. And when ++y gets evaluated, it takes that 4, and increments it by 1 giving you an immediate 5, and NOT the 4 that you think it is. After y++ is evalutated in that expression within the line itself, the value is seen as 3, but is incremented to 4 after, in which gets further incremented to 5 by ++y. You're both getting this wrong here... This is the way auto-incrementing operators actually work: Code: y=3
y++ --> value of y is still 3, increment happens later
{y incremented here: y is now 4}
++y --> value of y is 4 {auto-incremented to 5 during this evaluation}
x = 3 + 5 --> = 8I'm confused on why you both think it is (or should be) 7... As you can see in my example below, the incrementing is during evaluation. Whether we use the incremented value or the original before it is incremented is also determined. Code: int x = 5;
printf("x++: (Starting with x = %d)\n", x);
printf("\tEvaluation Time: %d\n", x++);
// x is incremented after the expression
printf("\tAfter %d\n", x);
x = 5;
// x is incremented during the expression
printf("\n++x: (Starting with x = %d)\n", x);
printf("\tEvaluation Time: %d\n", ++x);
printf("\tAfter %d\n", x);![]() Read my post again where I explain the math behind each way the compiler can interpret it: Quote:[y = 3] for ... x=(y++)+(++y): These are the same: Code: int x = 5;
printf("%d\n", ++x);
x = 5;
printf("%d\n", x=x+1);They both do this: Code: mov eax,dword ptr [x]
add eax,1
mov dword ptr [x],eax1. So at the time variable++ happens, the value of variable immediately after this, is variable + 1. 2. The variable which was incremented is then incremented and the incremented value is taken immediately as ++x, or x+=1/x=x+1 as shown above. This means that if variable was 3, the variable value after variable++ is 4. At the time of ++variable, that 4 is taken to 5, and that 5 is used in the expression. Thus... value = 3 + 5, not 3 + 4. I can prove it if you want. RE: Logic Unknown - help needed - Deque - 04-19-2013 (04-19-2013, 09:15 AM)ArkPhaze Wrote:(04-19-2013, 06:00 AM)Deque Wrote: So you just take as a given that the side effect of y++ takes place before the ++y is evaluated? You can't. You still don't understand what a sequence point is. The increment is not be ensured to happen right after, only after the sequence point. Quote:A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. The side effect can take place after the sum was created, thus x can get the value 7. I am not wrong. I told you to read it up and you didn't. It is there in the specification. Nothing to argue about. RE: Logic Unknown - help needed - Deque - 04-19-2013 (04-19-2013, 09:15 AM)ArkPhaze Wrote:(04-19-2013, 06:00 AM)Deque Wrote: So you just take as a given that the side effect of y++ takes place before the ++y is evaluated? You can't. You still don't understand what a sequence point is. The increment is not be ensured to happen right after, only after the sequence point. Quote:A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. The side effect can take place after the sum was created, thus x can get the value 7. I am not wrong. I told you to read it up and you didn't. It is there in the specification. Nothing to argue about. RE: Logic Unknown - help needed - Solixious - 04-19-2013 To the best of my knowledge, ArkPhaze is absolutely right. The value will never end up as 7 in the given case. It will always be 8. Code: y=3
x=(y++)+(++y);x=3+5=8 (for left to right) or x=4+4=8 (for right to left) The value of y will be 5 after the execution in any case. RE: Logic Unknown - help needed - Solixious - 04-19-2013 To the best of my knowledge, ArkPhaze is absolutely right. The value will never end up as 7 in the given case. It will always be 8. Code: y=3
x=(y++)+(++y);x=3+5=8 (for left to right) or x=4+4=8 (for right to left) The value of y will be 5 after the execution in any case. RE: Logic Unknown - help needed - Deque - 04-19-2013 (04-19-2013, 09:47 AM)Solixious Wrote: To the best of my knowledge, ArkPhaze is absolutely right. The value will never end up as 7 in the given case. It will always be 8. Did you even read my post? The side effect (which is the increment) of y++ can be delayed until the sequence points ends. That is after the sum was computed. You are assuming that the side effect takes place immediately, but that is by specification not ensured. RE: Logic Unknown - help needed - Deque - 04-19-2013 (04-19-2013, 09:47 AM)Solixious Wrote: To the best of my knowledge, ArkPhaze is absolutely right. The value will never end up as 7 in the given case. It will always be 8. Did you even read my post? The side effect (which is the increment) of y++ can be delayed until the sequence points ends. That is after the sum was computed. You are assuming that the side effect takes place immediately, but that is by specification not ensured. |