Previous + + and later + +-related cases ~~!!, Trigger murder

Source: Internet
Author: User

Previous + + and later + +-related cases ~~!!, Trigger murder

Let's take a look at a piece of amazing code:

int main(void){
int x = 4; int y; y = (x++); printf("hello world. y = %d.\n", y);  x = 4; y = (x++)+(x++); printf("hello world. y = %d.\n", y);   x = 4; y = (x++)+(x++)+(x++); printf("hello world. y = %d.\n", y);   x = 4; y = (x++)+(x++)+(x++)+(x++); printf("hello world. y = %d.\n", y);  x = 4; y = (++x); printf("hello world. y = %d.\n", y);  x = 4; y = (++x)+(++x); printf("hello world. y = %d.\n", y);    x = 4; y = (++x)+(++x)+(++x); printf("hello world. y = %d.\n", y);    x = 4; y = (++x)+(++x)+(++x)+(++x); printf("hello world. y = %d.\n", y); return 0;
     }

 

In VS2012 (windows Operating Environment), the test results are as follows:

\ + Y = 4.y = 8. // 4 + 4y = 12. // 4 + 4 + 4y = 16. // 4 + 4 + 4 + 4 \ front + + y = 5. y = 12. // 6 + 6y = 21. // 7 + 7 + 7y = 32. // 8 + 8 + 8 + 8

 

  

The test result on Ubuntu is:

// After + + y = 4.y = 9. // 4 + 5y = 15. // 4 + 5 + 6y = 22. // 4 + 5 + 6 + 7 // front + + y = 5.y = 12. // 6 + 6y = 19. // 6 + 6 + 7y = 27. // 6 + 6 + 7 + 8

 

The test results on windows are easy to understand:

1. For the post ++, x will not increase before the value is assigned. Therefore, adding an x ++ each time is a plus of four.

2. For the front ++, x has been incremented before the assignment, so the memory value corresponding to one more x is increased by 1, and then added.

Therefore, the result of 6*2 7*3 8*4 is displayed.

 

It is not easy to understand the test results on linux:

1. For post ++, the second result is different from that for windows. This is because linux uses the method of generating intermediate variables.

For example: y = (x ++) + (x ++); divided into multiple steps: 1) tmp = x; x = x + 1; 2) tmp1 = x; x = x + 1; 3) y = tmp + tmp1;

1) medium tem is equal to 4, and x is incremented; 2) medium tmp1 is equal to 4 and also increases by x; 3) in the result 4 + 5 = 9

When (x ++) increments to three, the analysis is the same, but an intermediate variable tmp2.

2. For the front ++, the third and windows results are different, which also produces intermediate variables:

For example, y = (++ x) + (++ x); is divided into multiple steps:

1) tmp = (++ x) + (++ x); 2) y = tmp + (++ x );

Thus, y = 6 + 6 + 7

For example, y = (++ x) + (++ x); is divided into multiple steps:

1) tmp = (++ x) + (++ x); 2) tmp1 = tmp + (++ x); 3) y = tmp1 + (++ x );

Thus, y = 6 + 6 + 7 + 8

The only confusing thing here is why (++ x) + (++ x) does not produce any intermediate variable? While (x ++) + (x ++) produces intermediate variables?

I guess because the first ++ should be incremental and then assigned a value, so it is directly x = x + 1, so there is no intermediate variable generated, and as the first

Two (++ x) and '+' generate an expression: (++ x) + (++ x ), this expression assigns an intermediate variable to function in sequence with the subsequent expression.

Summary:

1. For vs compilers, no redundant intermediate variables are generated in a statement, whereas ubuntu generates intermediate variables.

Therefore, in later ++, ubuntu has more operations on x's memory space, while in front ++, vs has more operations on x's memory space. Results are inconsistent.

2. After learning the first point, we should note that in a statement, do not perform multiple operations on a variable, because you do not know the compiler, how many intermediate variables will be generated for this statement, leading to a bloody situation ~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.