后置增量 i++ 何时执行?被处决?
可能的重复:
未定义的行为和序列点
在机器代码级别的 C++ 中,后自增 ++ 运算符何时执行被处决?
优先级表表明 postfix++ 运算符是级别 2:这意味着在
int x = 0 ;
int y = x++ + x++ ; // ans: y=0
The postfix ++ 中首先执行。
然而,这行代码的逻辑运算似乎是先进行加法(0+0),但这是如何发生的呢?
我的想象如下:
// Option 1:
// Perform x++ 2 times.
// Each time you do x++, you change the value of x..
// but you "return" the old value of x there?
int y = 0 + x++ ; // x becomes 1, 0 is "returned" from x++
// do it for the second one..
int y = 0 + 0 ; // x becomes 2, 0 is "returned" from x++... but how?
// if this is really what happens, the x was already 1 right now.
因此,另一个选项是虽然 x++ 在优先级表上比 x + x 更高,但由于 x++ 生成的代码被插入到加法操作的下方
// Option 2: turn this into
int y = x + x ; //
x++ ;
x++ ;
第二个选项似乎为了更有意义,但我对这里的操作顺序感兴趣。具体来说,x何时改变?
Possible Duplicate:
Undefined Behavior and Sequence Points
In C++ on a machine code level, when does the postincrement++ operator get executed?
The precedence table indicates that postfix++ operators are level 2: which means in
int x = 0 ;
int y = x++ + x++ ; // ans: y=0
The postfix ++'s execute first.
However, it would seem that the logical operation of this line is the addition happens first (0+0), but how does that happen?
What I imagine, is the following:
// Option 1:
// Perform x++ 2 times.
// Each time you do x++, you change the value of x..
// but you "return" the old value of x there?
int y = 0 + x++ ; // x becomes 1, 0 is "returned" from x++
// do it for the second one..
int y = 0 + 0 ; // x becomes 2, 0 is "returned" from x++... but how?
// if this is really what happens, the x was already 1 right now.
So, the other option is although x++ is higher on the precedence table that x + x, the code generated due to x++ is inserted below the addition operation
// Option 2: turn this into
int y = x + x ; //
x++ ;
x++ ;
That second option seems to make more sense, but I'm interested in the order of operations here. Specifically, when does x change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不会直接讨论 UB 示例的细节,而是讨论以下完美的示例:
现在,运算符的优先级意味着最后一行相当于:
而不是:
另一方面,后增量相当于两条单独的指令(从这里开始只是一个心理图片):
也就是说,原始表达式将被编译器解释为:
但只要满足以下约束,编译器就可以对 5 条指令重新排序满足(其中
x>y
表示x
必须在y
之前执行,或者x
先于y
):不同指令的执行顺序没有其他限制,因此以下都是有效的顺序:
Instead of jumping on the details of the example that is UB, I will discuss the following example that is perfectly fine:
Now, the precedence of operators means that the last line is equivalent to:
And not:
On the other hand, the semantics of the post increment are equivalent to two separate instructions (from here on is just a mental picture):
That is, the original expression will be interpreted by the compiler as:
But the compiler is allowed to reorder the 5 instructions as long as the following constraints are met (where
x>y
meansx
must be executed beforey
, orx
precedesy
):There are no other constraints in the order of execution of the different instructions, so the following are all valid sequences:
这
是未定义的行为。任何事情都可能发生,包括一些不合理的结果、程序崩溃或其他任何情况。只是不要那样做。
This
is undefined behavior. Anything can happen, including some unreasonable results, program crashing or whatever else. Just don't do that.
在 C++ 中,有一些称为“序列点”的东西。如果在没有中间序列点的情况下多次更改某个值,则行为为未定义。
考虑以下情况:
y 的值可以是 0、1 或其他完全随机的值。
底线是,不要这样做。这样做不会有什么好处。 :-)
In C++ there are things called "sequence points". If you alter a value more than once without an intervening sequence point, the behaviour is undefined.
Consider the following:
The value of y could be 0, 1 or some other completely random value.
Bottom line is, don't do it. Nothing good can come of it. :-)
在您的情况下,当您使用
x++
时,似乎会发生以下情况,
x
在操作完成后递增。然而
,不同的编译器会以不同的方式处理它,如果您在同一条语句中递增两次,您的应用程序可能会崩溃。
In your case, is seems like the following happens
When you use
x++
,x
increments after the operation is complete.While
However, different compilers will handle it differently and your application might crash if you increment twice in the same statement.