Thursday, February 20, 2014

Demonstrable bug in C#




int i = 381;
int j = 529;
i ^= (j ^= i ^= j);

In C, this works - even as i^=j^=i^=j;



Define "works". If you mean it produces the result you were expecting then that probably

depends on which compiler and version you're using at the time.



Order of evaluation

http://ift.tt/1gPgJ1y



Excerpts:



"Order of evaluation of the operands of any C operator, including the order of evaluation

of function arguments in a function-call expression, and the order of evaluation of the

subexpressions within any expression is unspecified (except where noted below). The compiler

will evaluate them in any order, and may choose another order when the same expression is

evaluated again."



"Undefined behavior"



"1) If a side effect on a scalar object is unsequenced relative to another side effect on

the same scalar object, the behavior is undefined."



i = ++i + i++; // undefined behavior

i = i++ + 1; // undefined behavior

f(++i, ++i); // undefined behavior

f(i = -1, i = -1); // undefined behavior



"2) If a side effect on a scalar object is unsequenced relative to a value computation using

the value of the same scalar object, the behavior is undefined."



f(i, i++); // undefined behavior

a[i] = i++; // undefined behavior



- Wayne


No comments:

Post a Comment