无限循环?

Bob*_*Bob 0 c++ infinite-loop while-loop

我在这个论坛上遇到过这个问题

#include <iostream>

using namespace std;

int main(int argc, char** argv) {

    int x=0;
    while (x<3) {
        x = x++;
        cout << x << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

鉴于上面的代码,为什么while循环无限?在mac os下使用gcc 4.4,while循环确实终止:)所以这个问题并不适用于所有架构.我变得艰难的输出是
1
2
3

我没有看到0,我猜原因与双重任务有关?

Bla*_*ear 14

x = x++;
Run Code Online (Sandbox Code Playgroud)

是未定义的行为

  • @voodoomsr:没有:http://stackoverflow.com/questions/98340/what-are-the-common-undefined-unspecified-behavior-for-c-that-you-run-into/98358#98358 (3认同)
  • @John:这意味着我们不能说为什么程序会这样或那样 (2认同)
  • @John:这意味着该程序无效,**任何**都可能发生. (2认同)