0

I came across this question in this forum

#include <iostream>

using namespace std;

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

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

    return 0;
}

given the code above, why is the while loop infinite? Using gcc 4.4 under mac os, the while loop does terminate :) so the question does not apply for all architectures. The output I get tough is
1
2
3

I don't see 0, and I guess the reason is related to the double assignment?

Bob
  • 10,741
  • 27
  • 89
  • 143

2 Answers2

14
x = x++;

is undefined behavior

BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • Which means that the loop is neither infinite nor finite? – John Mar 16 '11 at 21:42
  • 2
    @John: this means we cannot say why the program behaves this or that way – BlackBear Mar 16 '11 at 21:43
  • 2
    @John: Which means that the program is invalid, and **anyting** can happen. – Bo Persson Mar 16 '11 at 21:44
  • And note that really means **anything**. It could be that `x` winds up at the old value, or the new value, or some completely different value, or the program could crash, or the program could start [nethack](http://www.nethack.org/), or [demons could come flying out of your nose](http://www.catb.org/jargon/html/N/nasal-demons.html). – Anomie Mar 16 '11 at 21:50
  • that isn't undefined, is equal to x=x; x=x+1; – mjsr Mar 16 '11 at 21:54
  • @voodoomsr: no, the compiler is free to interpret it. for example it looks x = x = x + 1 to me – BlackBear Mar 16 '11 at 21:56
  • 3
    @voodoomsr: Nope: http://stackoverflow.com/questions/98340/what-are-the-common-undefined-unspecified-behavior-for-c-that-you-run-into/98358#98358 – John Mar 16 '11 at 21:57
  • @voodoomsr: Technically the operator ++ on an integer increments the variable but the result of the operation is the original value (see section 5.2.6 [expr.post.incr]) thus allowing it to be used in the expression. The assignment then happens after this. So its more like `int old = x;x=x+1;x = old;`. (so here we have an infinite loop). But there are no sequence points here so the compiler is free to re-order the statements. `int old = x;x=old;x=x+1;` or `x=x+1;int old=x;x=old;` – Martin York Mar 16 '11 at 22:11
  • @Martin, the teory says something different look: http://www.cplusplus.com/doc/tutorial/operators/ , do you have some reference that explain that wierd behavior (book, documentation site, not QA site)? it is some kind of empiric an non documented knowledge? – mjsr Mar 16 '11 at 22:35
  • @voodoomsr: I quoted you the reference in the *C++ standard* that explains *exactly* how ++ works. Now cplusplus.com is OK but its **NOT** a conical reference site. The undefined behavior means the optimizer is free to screw around how it pleases my interpretation is just three of many ways the optimizer can change things around. Yours interpretation is just as valid when it comes to UB. See [this answer for something similar](http://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviour-that-a-c-programmer-should-know-abo/367690#367690) – Martin York Mar 16 '11 at 22:48
  • @Martin chaging the reference for a better one -> "The C++ Programming Language, 3rd Edition" written by Stroustrup, in the chapter 6.2.5 it says this "The value of x++, however, is the old value of x. For example, y= x++ is equivalent to y=( t=x, x+=1, t), where t is a variable of the same type as x", Now i get why the loop. The optimizers that do no break this order of operations mantains x in zero. those that break that order increment x. It was fun, search the origin of this, :) – mjsr Mar 16 '11 at 23:27
  • @voodoomsr: Again "The C++ Programming Language, 3rd Edition" is nice but it is not the standard. You need to get a [copy of the standard](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents/4653479#4653479). – Martin York Mar 17 '11 at 00:33
0

you never see zero because the increment is before the cout.

mjsr
  • 7,410
  • 18
  • 57
  • 83
  • 1
    It is undefined behavior because the value of `x` is set twice between sequence points. The compiler is free to do the assignment and increment in any order, or really to do anything else that might make sense to it. – Anomie Mar 16 '11 at 21:58
  • i think different but im going to read further in the c++ manual that i have. Long time ago i learn that the POSTIncrement like this x=a++; means x=a; a=a+1; i dont get whay in this situation where a=x is differen – mjsr Mar 16 '11 at 22:04