"x = ++ x";真的不确定吗? [英] "x = ++x" is it really undefined?

查看:100
本文介绍了"x = ++ x";真的不确定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目上使用防止贪心来查找错误.

I am using Coverity Prevent on a project to find errors.

它报告此表达式的错误(变量名当然会更改):

It reports an error for this expression (The variable names are of course changed):

x=
   (a>= b) ?
   ++x: 0;

消息是:

EVALUATION_ORDER缺陷:在"x=(a>= b) ? ++x: 0;"中,"x"写在"x"(赋值LHS)中,并写在"(a>= b) ? ++x: 0;"中,但是副作用发生的顺序是未定义,因为没有中间的序列点.消息结束

EVALUATION_ORDER defect: In "x=(a>= b) ? ++x: 0;", "x" is written in "x" (the assignment LHS) and written in "(a>= b) ? ++x: 0;" but the order in which the side effects take place is undefined because there is no intervening sequence point. END OF MESSAGE

虽然我可以理解"x = x++"是未定义的,但对我来说这有点难.这是假阳性吗?

While I can understand that "x = x++" is undefined, this one is a bit harder for me. Is this one a false positive or not?

推荐答案

条件运算符?:在条件(第一个操作数)的评估与第二个或第三个操作数的评估之间具有一个序列点,但没有专用的序列点 之后的第二或第三操作数的求值.这意味着在此示例中,x的两个修改可能存在冲突(未由序列点分隔).因此,Coverity Prevent是正确的.

Conditional operator ?: has a sequence point between evaluation of the condition (first operand) and evaluation of second or third operand, but it has no dedicated sequence point after the evaluation of second or third operand. Which means that two modifications of x in this example are potentially conflicting (not separated by a sequence point). So, Coverity Prevent is right.

您在这方面的声明实际上等同于

Your statement in that regard is virtually equivalent to

a >= b ? x = ++x : x = 0;

具有与x = ++x中相同的问题.

现在,问题的标题似乎表明您不知道x = ++x是否未定义.确实是不确定的.出于与x = x++相同的原因,它是未定义的.简而言之,如果同一对象在一对相邻序列点之间进行了多次修改,则该行为是不确定的.在这种情况下,x通过赋值进行修改,并且通过++进行修改,没有序列点可以将这些修改彼此隔离".因此,行为是不确定的.在这方面++xx++绝对没有区别.

Now, the title of your question seems to suggest that you don't know whether x = ++x is undefined. It is indeed undefined. It is undefined for the very same reason x = x++ is undefined. In short, if the same object is modified more than once between a pair of adjacent sequence points, the behavior is undefined. In this case x is modified by assignment and by ++ an there's no sequence point to "isolate" these modifications from each other. So, the behavior is undefined. There's absolutely no difference between ++x and x++ in this regard.

这篇关于"x = ++ x";真的不确定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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