开发者

Compiler Optimization & Increments

Does compiler optimization cause a problem with code like this? Specifically, can the auto-increments be replied upon to evaulate in the correct order?

uint result =开发者_StackOverflow (array[i++] << 16) | (array[i++] << 8) | array[i++];


Yes; this is specified.

The spec says:

Operands in an expression are evaluated from left to right. For example, in F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to operator precedence.


No, I think you're modifying the object (i) multiple times between sequence points. IIRC neither bitwise or, nor array indexing are sequence points. You are only allowed to modify the object once, doing otherwise is unspecified or undefined behaviour.

NOTE. This is not necessarily an issue to do with the optimizer, it's just not legal.

Edit: Here's a link on sequence points - http://msdn.microsoft.com/en-us/library/d45c7a5d(VS.80).aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜