Harald van D?k
8/13/2008 9:05:00 PM
On Wed, 13 Aug 2008 13:54:41 -0700, DonStarr wrote:
> [...]
> arr[ len++ ] = func( len ); /* problem line */
> [...]
> It has been pointed out on the "other" mailing list that the function
> call results in a sequence point. I maintain that this sequence point is
> irrelevant, because:
> a) it can only "hit" when the operand on the RHS of the '=' operator
> is evaluated, and
> b) there's no guarantee that the operand on the RHS will be evaluated
> before the operand on the LHS.
This is mostly correct.
> I believe that, for the purposes of determining when the side-effect of
> the postfix operator ("len++") is "complete", the only sequence point
> that matters is the semicolon at the end of the statement. And, since
> <len> is both modified and read between that sequence point and the
> last, this is Undefined Behavior.
There are four relevant sequence points:
1- start
2- function call: func
3- function return: func
4- semicolon
The increment of len may happen either between 1 and 2, or between 3 and
4. If it happens between 1 and 2, then there is a problem. On the other
hand, if it happens between 3 and 4, then all is fine, because there will
have been a sequence point between the read and the store. However, there
is no requirement on implementations to choose one or the other, or to
document how the choice is made, and there is no possibility for a correct
program to detect which choice is made. Because of that, the program
should be modified so that the increment of len is guaranteed to follow
the call to func.
> Am I right?
Essentially, yes.