Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence points and side effects in C

In this C-FAQ it is give about sequence point;

The Standard states that;
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

In the examples

i = i++;
a[i] = i++;

it is clear from first sentence of the statement that these examples are results in undefined behavior.
In explaining second sentence of the statement it is said that;

second sentence says: if an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written. This rule effectively constrains legal expressions to those in which the accesses demonstrably precede the modification. For example, the old standby

 i = i + 1 

is allowed, because the access of i is used to determine i's final value. The example

a[i] = i++

is disallowed because one of the accesses of i (the one in a[i]) has nothing to do with the value which ends up being stored in i (which happens over in i++), and so there's no good way to define.

My questions are;
1.What does it mean by, if an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written.?

2.what does it mean by, The example a[i] = i++ is disallowed because one of the accesses of i (the one in a[i]) has nothing to do with the value which ends up being stored in i (which happens over in i++)
Could someone explain it in some easy way?

like image 910
haccks Avatar asked Jul 04 '13 15:07

haccks


People also ask

What are the sequence points in C?

The C language defines the following sequence points: Left operand of the logical-AND operator (&&). The left operand of the logical-AND operator is completely evaluated and all side effects complete before continuing. If the left operand evaluates to false (0), the other operand is not evaluated.

What are side effects in C?

The formal definition of side effect in the C language (C17 5.1.2.3/2) is rather: 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.

What is sequence point operation?

A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.

What is side effect of function explain?

A side effect is when a function relies on, or modifies, something outside its parameters to do something. For example, a function which reads or writes from a variable outside its own arguments, a database, a file, or the console can be described as having side effects.


1 Answers

My question are; 1.What does it mean by, if an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written.?

With a sub-expression like i++, i is written to. Moreover, assignment is an expression, so in i = 2, i is written to. It may not be immediately obvious that a = b is an expression, but it is. This is why you can do things like a = b = c, which is good, and if (a = b) which is less good.

So what it is saying is that if you write to i, with =, or pre- or post- increment, then any access to i must be as part of the calculation of the new value of i. However, and this is important, the only thing involved in the calculation of pre and post increment is the value of i at the start of the statement.

2.what does it mean by, The example a[i] = i++ is disallowed because one of the accesses of i (the one in a[i]) has nothing to do with the value which ends up being stored in i (which happens over in i++)

Precisely what it says. When you access i in a[i] it is not part of the calculation of the new value of i that results from i++.

Could someone explain it in some easy way?

Easy way: Don't use pre or post increment in an expression. always use them in statements by themselves. If you really really must, do NOT use the same variable anywhere else in the entire statement.

like image 154
Tom Tanner Avatar answered Sep 28 '22 08:09

Tom Tanner