Side effects of expressions

Source: Internet
Author: User

Expressions in C ++ can be divided into arithmetic expressions, relational expressions, logical expressions, and Other types. However, variables in expressions can be divided into two types: expressions with and without side effects.

1. The concept of side effects of Expressions generally indicates that when calculating the value of an expression, some variables need to be referenced. During the expression evaluation process, the values of these variables need to be extracted, but does not change the value of these variables. Such expressions are called non-side effects expressions. in the traditional sense, an expression is used for computing. In addition to generating a computing result, it should not change the value of any variable involved in the calculation process or produce other effects. in other words, traditional expressions should not have side effects. therefore, expressions in most advanced languages have no side effects. C ++ is a typical language that supports both advanced and low-level features. To improve the running efficiency, it references expressions with side effects. during the evaluation process, an expression not only references the variables used, but also changes their values. Such an expression is called an expression with side effects, or this expression has side effects.

2 analysis and understanding of the side effects of expressions, there are four expressions: 1, 5 * X; 2, X + Y; 3, X ++; 4. Y-= 18*2. expressions 1 and 2 are expressions without side effects, because the values of the referenced variables X and Y do not change during expression evaluation. For example, if X = 5 is known, Y = 10, expression 1 is 25, expression 2 is 15, X is 5, and Y is 10, expressions 3 and 4 have side effects, because the values of the referenced variables X and Y change during the expression value evaluation. for example, if the values of x and Y are 5 and 10 respectively, the value of expression 3 is 5, the value of X is 6, and the value of expression 4 is 26, the value of Y is changed to 26.

For the given expression, how can we correctly determine whether it is an expression with or without side effects? The main reason is the operator that appears in the expression. operators in C ++ can be divided into two types: operators without side effects and operators with side effects. if an expression references an operator with side effects, this expression is an expression with side effects. operations introduced by C ++ with side effects and Corresponding operators include: (1) Value assignment (= ). (2) compound assignment (+ =, 1:, * =,/=, % =, "=," = ,! =, & =, 1 ). (3) increase 1 before, minus 1 (++, one by one) before ). (4) increase by 1 and then minus 1 (++, one by one ). these operations must act on variables. Therefore, these operations can also be called operations on variables. for operations acting on variables, in addition to the increment of 1 and the increment of 1, there is another characteristic: the value of the expression as the result of the operation is the value obtained by the variable. for example, execute the statement sequence: int I = 5; count <(I + = 3); count <I, the last two output results are 8. that is to say, the value of the expression is stored in the variable to which the expression is applied. for the operators after adding 1 and minus 1, although the value of the variable is different from the value of the expression after the operation (the value of the expression cannot be expressed by a variable), the value of the expression is calculated, make sure that the value of the variable has changed, such as the execution statement sequence: int I = 5; cout <I ++; count <I; the last two output results are 5 and 6 respectively. Therefore, starting from the concept of an expression with side effects, the following addition 1 and minus 1 operators still have side effects .. the difference between an expression with side effects can also be compared from the computational order. values of several expressions without side effects are not different in the order of calculation. For example, set X = 3, Y = 2, then the values of 3 * x and expression X + Y must be 3*2 = 6 and 3 + 2 = 5; however, the order of calculation between expressions with side effects and other expressions is very important, and the results of different orders are also different. For example, if X = 3, Y = 2, then (++ X) after + Y and X + Y ++ are calculated in sequence, the values of the expressions are 6 and 6 respectively, while the two expressions change the calculation order and are calculated first. X + Y ++, after calculation (++ X) + Y, the expression values are 5 and 7, respectively. therefore, when calculating multiple expressions with side effects, pay attention to the order of expressions.

3. Side effects of pointer expressions some pointer expressions also have side effects. The cause of side effects is that operators with side effects are used. in pointer-related operations, ++ (first increment 1), one by one (first minus 1), + =,-=, and = are used to change a variable and its value. The new value obtained by the variable is the value of the pointer expression, therefore, the value of the expression is provided indirectly in the form of variables. that is to say, the operation results (that is, the value of the pointer expression) of these five operators are treated as variables, you can continue to perform operations that can only be performed on variables (including the preceding five operations, as well as obtaining the address, adding 1 after, and reducing 1 after ), therefore, expressions such as ++ (++ pk) and pk + = & k are valid (where pk is a pointer variable ). for ++ (after adding 1) and one-to-one (after minus 1) operations, although they also apply to variables and change their values, but the value of the pointer expression is different from the new value obtained by the variable. Therefore, the value of the expression cannot be provided in the form of a variable, but can only be provided in a more direct form of a value. that is to say, the operation result after adding 1 (or minus 1) is treated as the address value in the numerical form of the constant, so it is like (pk ++) ++, all expressions such as pk-10 = & k are invalid (pk is a pointer variable ). * (indirect access) the operation result is regarded as a variable. You can continue to perform operations that can only be performed on the variable. & (take the address) Acts on the variable, but does not change the value of the variable, the operation result is treated as an address value in the numerical form of a constant. You cannot perform operations that can only be performed on variables. both operators have no side effects. in short, the conclusions related to the side effects of numeric expressions are also applicable to pointer expressions. operators with side effects in Pointer expressions are the same as operators with side effects in numeric expressions.

4. Application of side effects of expressions
4.1 A comma-separated expression is a comma-separated expression, that is, expression 1, expression 2 ,? ? , Expression n, comma expression value order is: first calculate the value of expression 1, then calculate the value of expression 2, and so on, finally calculate the value of expression n, the value of the final expression n is used as the value of the entire comma expression. for example, w --, j + = 3, k ++, P = w + j + k is a comma expression. If the initial state is w = 5, j = 6, k = 7, then the value expressed by this comma is 21 (5 + 9 + 7 ). for each expression in a comma expression, except the last one, it must be an expression with side effects. For example. the expressions 6 + 18 and 72*9 in the comma expression like X = 5, 6 + 18, 72*9, and Y = 10 have no side effects, and are meaningless During computation.

4.2 expression statement in C ++, any expression can be used as a statement, called an expression statement. The method is to add the statement terminator (I .e.;) after the expression ;). the reason why C ++ expressions can be used as statements is that C ++ expands the concept of expressions and classifies operations with negative effects such as value assignment into the scope of expressions. all input and output operations are implemented through functions. There are no special input and output statements, and function calls are also a form of expressions. therefore, in addition to flow control statements, other functions such as assignment, input, and output can only be implemented through expressions. when an expression is used as a statement, its value is discarded and not used. Its statement function is reflected by its side effects. Therefore, it is meaningless to use an expression without side effects as a statement, for example, the expression statement K + 19; the expression value (the sum of K and 19) is discarded. It is a meaningless statement. Only expressions with side effects can be used as statements, like the expression statement x + = 5; the expression value is discarded and not used, but because ++ = is a side-effect operator, after calculation, the value of the expression is actually saved in the variable X, therefore, the expression statement is meaningful. in short, the side effects of expressions in C ++ are not necessarily bad. Only by understanding the concept of expressions and understanding operators with side effects can they be well handled, it brings high operational efficiency to self-designed programs.

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.