T
Tim Rentsch
Spiros Bousbouras said: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++".
If a==INT_MAX then a++ produces undefined behavior so all
implementations are valid.
What you mean is that an implementation could exhibit any
particular behavior and still be a conforming implmentation.
If an implementation chooses to define the behavior on overflow as
doing a particular something (which the implementation is free to
do), and then does something else, it seems fair to say the
implementation is not conforming, at least in spirit if not in
letter. (I can't find any statement in the Standard that says the
required documentation for extensions and implementation-defined
behaviors must be accurate, but certainly it seems reasonable
that such accuracy is expected.)