序列点和副作用用C [英] Sequence points and side effects in C

查看:87
本文介绍了序列点和副作用用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此 C-FAQ 它提供有关的序点;


  

标准指出;结果
  的previous和下一个序列点之间的对象应具有其存储的值在由前pression评价修改最多一次。此外,前一个值是唯一的访问以确定被存储的值。


在实施例

  I = I ++;
一个由[i] =我++;

这是从这些例子的结果的未定义行为语句的第一句话清楚。结果
在解释声明中的第二句是说;


  

第二句说:如果对象是一个完整的前pression中写到,任何和所有访问它同前pression内必须直接参与的价值来计算被写入。此规则有效地限制了法律前pressions到那些其中访问demonstrably precede修改。例如,旧的备用


  I = I + 1


  

是允许的,因为我的访问被用于确定我的最终值。这个例子


  A [i] =我++


  

是不允许的,因为我的访问之一(一个在[I])无关与存储在I(这在我+ +发生过),其最终值,所以没有不错的方式来定义。


我的问题是;结果
1.什么意思用,如果对象是一个完整的前pression中写到,任何和所有访问它同前pression内必须直接参与价值的计算要写入。

2.什么意思通过,这个例子 A [i] =我++
是不允许的,因为我的访问之一(一个在[I])无关,与存储在我(恰好在以上我++)结果这最终的价值
可能有人以某种简单的方式解释一下吗?


解决方案

最后,我对SO有一个解释这个点。看完之后和常见问题解答 我的结论的;

1,最后一句


  

此外,前值应只访问,以确定该值将被存储


会是这样;


  

此外,前值的对象的应只访问以确定在修改/新值(同一对象的 )将被存储。


由于它是由明确的例子

  INT I = 1,J,A [5];
 I = I + 1;
 J = 1 + 1;
 一个[我] =我;

在EX pression的情况下, I = I + 1 前值(即 1 点击这里)的 I (中右)访问,以确定 i的值来存储,这就是声明


  

如果一个对象是一个完整的前pression内写入,任何和所有访问它同前pression必须直接参与的值的计算中被写入
  。


表示。结果
 而在 J = + 1 A [i] = I 的访问值<$ C的情况下, $ C> I 是只值不可以的值,因为没有其中, I 修改这些声明。

结果;

2,第二个问题可以作为解释
在EX pression A [i] =我++ A [I +] =我,第一句的情况下,以上声明


  

的previous和下一个序列点的对象应具有其存储的值由前pression评价修改最多一次之间。


获取失败 I 终止的修改只有两个连续序列点之间一旦的。这就是为什么我们需要第二句。结果
这两个例子都是用C不允许的,因为的前值i 访问两次即我++ 本身获得的前值>我在EX pression对其进行修改和>前值/ 的,因此其他访问 I ,因为它不被访问,以确定要存储的修改后的值是不必要的。

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?

解决方案

Finally I got an explanation on SO about this point . After reading it and FAQ I concluded that;

1.The last sentence

Furthermore, the prior value shall be accessed only to determine the value to be stored

would be like this;

Furthermore, the prior value of an object shall be accessed only to determine the modified/new value( of same object ) to be stored.

As it is clear by the example

 int i = 1, j, a[5];    
 i = i + 1;
 j = i + 1;
 a[i] = i; 

in case of expression i = i + 1 prior value (which is 1 here) of i (in R.H.S) is accessed to determine the value of i to be stored and this is what the statement

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 .

says.
While in case of j = i + 1 and a[i] = i, the accessed value of i is just value not prior value as no where i is modified in these statements.

2.The second question can be explained as;
In case of expression a[i] = i++ or a[i++] = i, first sentence of above statement

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.

get failed as i is modified only once between two consecutive sequence point. And that's why we need second sentence.
Both of these examples are disallowed in C because the prior value of i accessed two times i.e, i++ itself access prior value of i in the expression to modify it and hence other access of prior value / value of i is needless as it is not accessed to determine the modified value to be stored.

这篇关于序列点和副作用用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆