C language side effect and sequence point

Source: Internet
Author: User

Http://hi.baidu.com/ljy2008wl/blog/item/9990b01d37dee448f3de32b0.html

In C, 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 refers to a special time point in the program running. All the side effects before this point have ended, and subsequent side effects have not yet occurred.
The end mark of the C statement-semicolon; and parentheses () are sequence points. 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. The order in which side effect occurs is not specified and is determined by the compiler. This isUnspecified, which corresponds to specified (specified). We will talk about some operators that contain sequence points in the future. The time point at which any full expression operation ends is also a sequence point. The complete expression is not a subexpression. 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 ++; // Error

Y = (x ++) + (x ++); // correct
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. The standard stipulates that the value saved by an object can be modified only once between two sequence points.
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! Compilation of this Code on different compilers may cause
The value is different. 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.