Timing points and side effects in C/C ++

Source: Internet
Author: User

Http://blog.csdn.net/jerry_chow/article/details/2579834

In C ++, seqence point and side-effect are two related concepts, after understanding the side effects, you can better understand another concept in C ++-time series. The understanding of these two points is of great help to debug and eliminate code ambiguity (ambiguity.

Introduction: When a class was completed last week, the class was overloaded by operators. In order to make the operator overload more intuitive, I wrote the following statement in the test code: I = I ++; that is, this statement leads to this article, horrible code! I don't know whether this short piece of code hides an error. In short, this piece of code produces undefined results for C and C ++. You can test the Code as follows:
// Platform: WINXP + vc60 # include <iostream> using namespace STD; void main () {int I = 1; int S = 0; S = I ++; cout <S <Endl; // here S = 1 Int J = 1; j = J ++; cout <j <Endl; // here J = 2, what happens? }/* Output: 12 */

 

There should be nothing surprising about the output of the first S. The Postfix ++ operator assigns the right-value to s before increasing the value of I to 1, so output 1 is returned.

However, if we think about the output of the second J in the same way, the result should also be 1 (after J increments, it is assigned a value by the previous right-value, in the end, its value becomes 1 ). But the actual output result is 2. In fact, for J at this time is 1 or 2, it is true for compiler. An undefined result is generated here. For some operations in C ++, the C ++ standard is not forcibly defined, but left to each compiler for self-determination. For example, the order in which function parameters are evaluated. Generally, this is not a problem, but it may also cause ambiguity. For example, fun (I ++, I), whether to execute I ++ first or I first, each compiler has its own implementation. Different implementations can produce different results. Side-effect:The general operator will not change the value of the variable involved in calculation. Any operator that changes the operand operation will produce side effects .; For example, S = x + y. The "+" operator does not change the value of X or Y. In C/C ++ expressions, the evaluation of expressions may change the values of the variables involved in calculation due to the intervention of ++, --, and other operators. This produces side effects. Not all side effects will affect the program results. (note, "=" indicates that the side effects are intentionally used to change the value of the operand S. We will ignore the "=" Side effects): S = (I ++) + (J ++); this code has two side effects, they are I ++ and J ++. However, executing I ++ first or J ++ first does not affect the program results. The result is equal to S = I + J. However, if the operation result is affected by the execution sequence of the side effects, the problem arises. For example: S = I + (I ++); execute I first, or execute I ++ first (that is, execute side effects first, or then execute side effects ), the calculation results will be completely different. In order to determine the execution sequence of a statement, C ++ introduces sequence point ). Sequence point:Time Series point definition: at certain specified points in the execution sequence called 'sequence points', all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have take place. A Time Series point is defined as a point in the Program Execution Process: All side effects of the expression before the point are completed before the program execution reaches the point; all side effects of the expression after the point, it does not occur when the program is executed to this point. For a piece of C ++ code, the C ++ compiler divides it into boxes one by one during compilation and then executes them sequentially, that is, for the following statement blocks: {++ X; + + Y;} generates an execution sequence, where the red dots mark three time series points A, B, and C. Let's take time series B to explain. The rest is the same. The side effects (+ + x side effects) produced by A-B are completed when running to B points. Side effects of B-C have not yet occurred. The execution sequence of side effects between two time series points is undefined. That is to say, if there are two or more side effects between two time series points, the timing of these side effects is not fixed. If the value of an expression depends on the order of these side effects, the value of the expression is also variable. Then how does the C ++ compiler decide whether to generate a time point? C ++ places time series points in the following locations: 1, the point of calling a function, after evaluating its arguments. 2, the end of the first operand of the & operator. 3, the end of the first operand of the | Operator. 4, the end of the first operand of?: Conditional operator. 5, the end of the each operand of the comma operator. 6, completing the evaluation of a full expression. they are the following: A, evaluating the initializer of an auto object. b, the expression in an 'ordinary' statement-an expression followed by semicolon. c, the controlling expressions in do, while, if,
Switch or for statements. d, the other two expressions in a for statement. e, the expression in a return statement. let's take two statements for analysis. Let's take a simple note: F = a + B; according to the 6th B, we can know a typical example: "; "A Time Series point will be inserted after the end of a statement. Another example is: If (A> B) n = 5. According to the C of the first 6th, a time series point is inserted after A> B. Back to question:Let's go back to the initial issue. If we write the following statements in the class, all the undefined behaviors will occur: X [I] = I ++; F = (I ++) + I; fun (I, I ++); I = I ++; fun (methoda (), methodb ()); // methoda and methodb may also have side effects, depending on whether they directly (or indirectly) change the variables to be accessed by the other party or fun. these statements have such characteristics: 1. Operators (Operations) that produce side effects in the statements ). 2. The running result depends on the execution sequence of the statements that produce side effects.
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.