Sequence Point

In c and C++, there is a sequence point at ; and {} and also at various operators (but not all operators) &&, || and ?:

A variable may be written to only once per sequence point, and must not be read if written to other than to determine the value to write, otherwise the behaviour is undefined (by the language spec). Also, evaluation order within two sequence points is undefined (although operator precedence is defined). This is already quite confusing so an example, from ThreeStarSwap:

  a^=b^=a^=b; 

is undefined behaviour

  a^=b;b^=a;a^=b; 

is defined;

note that this is not a problem caused by the precedence order of operators so

  (a^=(b^=(a^=b)));

is still undefined behaviour.

Also, given

  x = f() + g() * h();

the multiplication will be performed before the addition (and assignment) opperators. However, the order that f() g() and h() are evaluated in is undefined.

I hope that is clear.

Another case which is probably more clear:

  a = c++ + c++;

What does a become? 2*c, 2*c+1 or what else? It is undefined.


EditText of this page (last edited November 13, 2008) or FindPage with title or text search