Side effects and sequence points

Source: Internet
Author: User
Reposted from: guiwaa's blog
Terms in C LanguageSide effects(Side effect) refers to the modification of data objects or files. For example, the following statement

Var = 99;

The side effect is to change the VaR value to 99. Evaluate the expression may also produce side effects, such:

Se = 1, 100

The side effect of this expression is that the SE value is modified to 100.

Sequence point(Sequence point) refersProgramA special time point during running, all side effects before this point have been completed, and subsequent side effects have not yet occurred.

End mark of the C statement-semicolon (;) Is a sequence point. That is to say, the side effects caused by the assignment, auto-increment, or auto-subtraction in the C statement must end before the semicolon. We will talk about some operators that contain sequence points in the future. AnyComplete Expression(Full expression) the time point at which the operation ends is also a sequence point. The so-called complete expression, that is, this expression is notSubexpression. The so-called subexpression refers to the expression in the expression. For example:

F = ++ E % 3

The entire expression is a complete expression. In this expression, ++ E, 3, and ++ E % 3 are all its subexpressions.

With the concept of sequence points, we will analyze a common error:

Int x = 1, Y;
Y = x ++;

Here, y = x ++ is a complete expression, and X ++ is its subexpression. The end of this complete expression operation is a sequence point, where int x = 1, Y; is also a sequence point. That is to say, X ++ is located between two sequence points. Standard provisions,Between Two sequence points, the value saved by an object can only be modified once.. But we can see clearly that in the above example, the value of X is modified twice between two sequence points. This is obviously wrong! This sectionCodeCompiling on different compilers may lead to different values of Y. The common result is that the value of Y is changed to 2 or 3. Here, I am not going to make a more in-depth analysis on this issue. You only need to remember that this is wrong and you can simply stop using it. If you are interested, you can refer to the relevant information listed below.

The C language standard defines side effects and sequence points as follows:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. evaluation of an expression may produce side effects. 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 taken place.

Translation:

Accessing variable objects, modifying objects or files, or calling functions that contain these operations are all side effects and change the status of the execution environment. Computing expressions can also cause side effects. Some specific points in the execution sequence are called sequence points. At the sequence point, the side effects of all operations before this point should end, and the side effects of subsequent operations have not yet occurred.

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.