增量递减运算符的奥秘? [英] Increment decrement operator mystery?

查看:136
本文介绍了增量递减运算符的奥秘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc\">Could谁能解释这些不确定的行为,(I = I + + + + I,I = I ++等&hellip;)结果
  未定义行为和顺序点

不管我多么努力理解他们的行为,我永远不会成功。今天我遇到了另一个例子。

No matter how much I try to understand their behavior, I never succeed. I encountered another example today.

void fun(int x, int y, int z){
    cout<<x<<endl<<y<<endl<<z;   
}

int main(){
    int x=10;
    fun(++x, x++, ++x);
}

这我$ P $的输出pdicted是11,11,13,但是,其13,11,11,任何人都可以解释如何?更重要的是,有一个傻瓜证明的方式来理解这些运营商。就像,重写以其他方式除权pression。如何编译器实际上codeS它们将也有帮助。

The output of this I predicted to be 11, 11, 13. But, its 13, 11, 11. Can anyone explain how? More importantly, is there a fool proof way to understand these operators. Like, rewriting the expression in some other way. How compiler actually codes them will also help.

推荐答案

有没有什么都懂,至少在C ++的行为是,因为它的未定义。什么事情都可能发生。这里有两个问题:

There is nothing to understand about the behaviour in C++ at least, since it is undefined. Anything could happen. There are two issues here:


  1. 函数参数的评估顺序为未指定,这意味着排序可以是任何东西,不能依靠。

  2. 您正在修改和读取变量 X 多次没有任何干预序列点。这导致的未定义行为

  1. The order of evaluation of function arguments is unspecified, meaning the ordering could be anything and cannot be relied on.
  2. You are modifying and reading variable x multiple times without any intervening sequence points. This leads to undefined behaviour.

这篇关于增量递减运算符的奥秘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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