Jerry Coffin wrote:
> In article <d16e4f6a-c4f2-4b6f-bf7d-
> ea1258da781c@[EMAIL PROTECTED]
>, james.kanze@[EMAIL PROTECTED]
> says...
>> On 9 mai, 19:21, "Victor Bazarov" <v.Abaza...@[EMAIL PROTECTED]
> wrote:
>
> [ ... ]
>
>>> For example, it's not undefined behaviour to do
>>
>>> f(i++) + ++i;
>>
>> Yes it is, since there is no sequence point between the two
>> incrementations. Sequence points only define a partial
>> ordering: there is a sequence point before calling f, but that
>> only establishes and ordering between i++ and the call to f; it
>> doesn't establish any ordering between the two incrementations.
>
> I believe it establishes _some_ ordering, but not enough[1]. In
> particular, I believe the compiler is required to treat evaluation
> of a function argument and calling the function as atomic -- i.e.
> once the evaluation of any argument takes place, it must proceed to
> evaluated the other arguments (if any) and then call the function.
>
> In the expression above, I don't believe it's allowed for the post-
> increment to be evaluated, then the pre-increment, then the function
> call. It is, however, allowed for the pre-increment, then the post-
> increment, then the function call -- and in this ordering, there is
> no sequence point between the pre-increment and the post-increment,
> so the result is undefined behavior.
>
> 1: Though it's open to some question. $1.9/8 says: "Once the
> execution of a function begins, no expressions from the calling
> function are evaluated until execution of the called function has
> completed."
>
> I'm interpreting evaluating the arguments to a function as part of
> execution of the function. If you choose to interpret it as a
> completely separate act that happens before the function's
> execution, then you're right -- no ordering is defined. At least in
> this case, it doesn't make any real difference though -- the
> overall result is undefined behavior either way.
I think your second interpretation is the correct one. It means that
++i can be evalueated before or after calling f, but not during.
However, evaluating the arguments is done before calling the function,
not as part of the call.
Breaking it down:
evaluating i++
sequence point
call function
return
sequence point
add
At the point of the add operation, the result of the right hand side
of the addition must be available. It must be evaluated somewhere
before the add operation, obviously, but not between the two sequence
points.
Bo Persson


|