On 3/8/2011 1:09 PM, Chad wrote:
[...]
The part I'm kind of fuzzy about is the ++b
in
int k = a++ + ++b;
The result of the preincrement operator to (applied) b is also a side
effect. However, I think this side effect is applied before the
statement completes. Ie, both b in incremented by one and the side
effect is applied to b. Whereas side effect of a++ is applied *after*
the statement completes.
No. Both the increments of a and of b are guaranteed to take place before
the statement completes. (Or, more technically, before the next "sequence
point".) When, exactly, during the execution of the statement, the
increments take place is not specified by the Standard.
However, the meaning of "a++" is "the value of a before the increment", and
"++b" is "the value of b after the increment". However, that does not
necessarily mean that "b" actually has been incremented by the time the
value of "++b" is used, nor that "a" won't be incremented before the value
is read.
In other words, this pseudo-code is a perfectly valid implementation of the
above code:
; extern int a,b;
; int k = a++ + ++b;
load k,a
add k,b
incr k
incr a
incr b
In fact, I believe this implementation is just as valid:
incr a
load k,a
add k,b
incr b
Despite the fact that we are using the value of "a++" after the increment,
and "++b" before the increment, the result is indistinguishable from the
other way around. (However, there may be a problem with this implementation
for values of a==INT_MAX and b<0 due to the overflow in "a++".