C中的序列点和副作用

前端之家收集整理的这篇文章主要介绍了C中的序列点和副作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在这 C-FAQ中,它是关于 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.

在例子中

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

从声明的第一句可以清楚地看出,这些例子是未定义行为的结果.
在解释声明的第二句时,据说;

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.

我的问题是;
1.它是什么意思,如果一个对象被写入一个完整的表达式,那么在同一个表达式中对它的任何和所有访问必须直接参与计算要写入的值.

2.它是什么意思,例子a [i] = i
是不允许的,因为i的一个访问(a [i]中的一个)与最终存储在i中的值无关(在i中发生)
有人可以用一些简单的方法解释它吗?

解决方法

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.?

使用像i这样的子表达式,我被写入.而且,赋值是一个表达式,所以在i = 2中,我被写入. a = b是表达式可能不是很明显,但确实如此.这就是为什么你可以做一些事情,比如a = b = c,这是好的,如果(a = b)哪个不太好.

所以它所说的是,如果你写入i,或者,或者前后增量,那么对i的任何访问必须作为i的新值的计算的一部分.然而,这很重要,计算前后增量所涉及的唯一事情是在语句开头的i的值.

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++)

正是它所说的.当你在[i]中访问i时,它不是i的新值的计算的一部分.

Could someone explain it in some easy way?

简单方法:不要在表达式中使用前置或后置增量.总是在声明中自己使用它们.如果你真的必须,请不要在整个语句中的任何其他地方使用相同的变量.

猜你在找的C&C++相关文章