序列点和偏序 [英] Sequence points and partial order

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

问题描述

几天回来有一个讨论这里是否除权pression

A few days back there was a discussion here about whether the expression

我= + I + 1

i = ++i + 1

调用UB
(未定义的行为)或没有。

invokes UB (Undefined Behavior) or not.

最后得出结论有人提出,它调用UB为'i'是改变不是两个序列点之间再一次的值。

Finally the conclusion was made that it invokes UB as the value of 'i' is changing more than once between two sequence points.

我参与了讨论与约翰内斯绍布在同一个线程。据他介绍

I was involved in a discussion with Johannes Schaub in that same thread. According to him

I =(I,I + +,我)+1 ------(1)/ *调用UB和* /

i=(i,i++,i)+1 ------ (1) /* invokes UB as well */

我说(1),因为previous SUBEX pressions的副作用是由逗号运算清除不调用UB,我的和我++和我++之间我。

I said (1) does not invoke UB because the side effects of the previous subexpressions are cleared by the comma operator ',' between i and i++ and between i++ and i.

然后他给了如下解释:

是的顺序点我++之后完成之前所有的副作用,但没有什么是停止分配副作用与我++副作用重叠。的根本问题是,转让的副作用不指定后或转让的两个操作数,所以序列点的评估不能对于保护这一做任何事情之前,发生:序列点诱发偏序:正因为有后前一个序列点i ++不指所有副作用关于测序以我

"Yes the sequence point after i++ completes all side effects before it, but there is nothing that stops the assignment side effect overlapping with the side effect of i++.The underlying problem is that the side effect of an assignment is not specified to happen after or before the evaluation of both operands of the assignment, and so sequence points cannot do anything with regard to protecting this: Sequence points induce a partial order: Just because there is a sequence point after and before i++ doesn't mean all side effects are sequenced with regard to i.

另外注意,仅仅是一个顺序点意味着什么:评估的顺序不是由code的形式所决定的。它是由语义规则决定的。在这种情况下,不存在语义规则说当分配副作用方面恰好评估其两个操作数或这些操作数SUBEX pressions的

Also, notice that merely a sequence point means nothing: The order of evaluations isn't dictated by the form of code. It's dictated by semantic rules. In this case, there is no semantic rule saying when the assignment side effect happens with regard to evaluating both of its operands or subexpressions of those operands".

写在大胆的说法混淆了我。据我所知:

The statement written in "bold" confused me. As far as I know:

在执行顺序称为序列点某些特定点,previous评估所有的副作用应完整及后续评估的无副作用应该发生。

因为,逗号运营商也指定我的副作用++,当我们到达最后i.He(约翰)已经取消执行顺序将是正确评价过的顺序没有规定被(但在的情况下,逗号操作符是很好规定)。

所以,我只是想知道是否(1)将调用UB与否?有人可以给另一个有效的解释吗?

So I just want to know whether (1) invokes UB or not?. Can someone give another valid explanation?

谢谢!

推荐答案

C标准说,这个关于赋值运算符(C90 6.3.16或C99 6.5.16赋值运算符):

The C standard says this about assignment operators (C90 6.3.16 or C99 6.5.16 Assignment operators):

更新左操作数的存储值previous和下一个序列点之间不应出现的副作用。

The side effect of updating the stored value of the left operand shall occur between the previous and the next sequence point.

在我看来,在语句:

i=(i,i++,i)+1;

顺序点'previous赋值操作符将是第二个逗号操作符和'下一个'序列点会是前pression的结束。所以我说,前pression不会调用未定义的行为。

the sequence point 'previous' to the assignment operator would be the second comma operator and the 'next' sequence point would be the end of the expression. So I'd say that the expression doesn't invoke undefined behavior.

不过,这当然pression:

However, this expression:

*(some_ptr + i) = (i,i++,i)+1;

将有不确定的行为,因为2个操作数赋值运算符的计算顺序是不确定的,在这种情况下,而不是问题是当赋值运算符的副作用发生,问题是你不知道i在左手柄操作使用的值将之前或右侧后进行评估。评价问题的这一顺序不会因为这样的前pression i的值实际上不是在左侧用发生在第一个例子 - 所有的赋值运算符是感兴趣的是左值性

would have undefined behavior because the order of evaluation of the 2 operands of the assignment operator is undefined, and in this case instead of the problem being when the assignment operator's side effect takes place, the problem is you don't know whether the value of i used in the left handle operand will be evaluated before or after the right hand side. This order of evaluation problem doesn't occur in the first example because in that expression the value of i isn't actually used in the left-hand side - all that the assignment operator is interested in is the "lvalue-ness" of i.

但我也认为,这一切就足够了粗略的(和我所涉及的细微差别的理解足够粗略的),我不会感到惊讶,如果有人能说服我,否则(无论是数量)。

But I also think that all this is sketchy enough (and my understanding of the nuances involved are sketchy enough) that I wouldn't be surprised if someone can convince me otherwise (on either count).

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

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