C中的序列点和副作用

C中的序列点和副作用,第1张

概述在这 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. Fu 在这 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中的序列点和副作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1258143.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存