未定义行为和顺序点 [英] Undefined behavior and sequence point

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

问题描述

从过去的几天里,我试图了解未定义行为。前几天我发现了一个 C-FAQ 链接。这有很大帮助清除很多困惑,但是当我阅读问题创建另一大混乱# 3.8 。经过我很多的努力,理解语句(特别是第二句);

From past few days I was trying to learn about undefined behavior. Few days ago I found a c-faq link. This helps a lot to clear many confusions, but creates an another big confusion when I read the question #3.8. After my lots of efforts to understand the statement (specially second sentence);

本标准规定

的previous和明年顺序点对象所之间的由前pression评价修改最多一次存储的值。此外,前一个值是唯一的访问以确定被存储的值。

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 felt better to ask this question on SO but none of the answer there explained the second sentence of this statement. Finally, I got an explanation there about this point . After reading it and FAQ many times I concluded that;

1,最后一句

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

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

会是这样;

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

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

由于它是由明确的例子

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

在EX pression的情况下, I = I + 1 前值(即 1 点击这里)的 I (RHS中)被访问以确定 i的值来进行存储。而在 J = + 1 A [i] = 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. 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.

EX pression A [i] =我++ A [I +] =我,上述声明的第一句话

2.In case of expression a[i] = i++ or a[i++] = i, first sentence of above statement

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

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

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

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.

当我想出了前pression I = I + 有关它在C-FAQ中表示。

The problem starts when I came up with the expression i = i++ about which it is stated in c-faq

其实,其他前$ P $我们一直在讨论pssions违反第二句话,也是如此。

我想在这个前pression I (中右)访问,以确定的修改后的值i 。结果
如何这个前pression违反第二条语句?

I think in this expression i (in R.H.S) is accessed to determine the modified value of i.
How this expression is violating the second statement?

推荐答案

想想这种方式:

a = i++;

相当于:

a = i;
i++;

在访问 I 中的增量无关确认哪些价值将被存储到的值。因此, I = I ++ 包含的两处修改我(这是由第一句话不允许),但还可以, I = 修改 I 是独立于访问中的一个 I 我++

The accesses the value of i in the increment has nothing to do with determining what value a will be stored into a. So i = i++ contains two modifications of i (which is disallowed by the first sentence), but also, the i = modification to i is independent from one of the accesses to i in i++.

我觉得有人只是被额外聪明那里。有没有必要弄清楚一个未定义的行为有多少是不确定的。修改值两次就够了不确定的。

I think someone was just being extra clever there. There's no need to figure how much an undefined behavior is undefined. Modifying a value twice is enough undefined.

这篇关于未定义行为和顺序点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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