Re: Function call evaluation order

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Fri, 24 Mar 2006 20:57:33 -0800, Tim Roberts <timr@xxxxxxxxx> wrote:

"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote:
"Tim Roberts" <timr@xxxxxxxxx> wrote:
"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote:
"Cheng" <chengwuchew@xxxxxxxxxxx> wrote:

s.assign(data[i++]).append(data[i++]).append(data[i++]);

Not only would it be better - you must do so. The statement shown
above exhibits undefined behavior. There exists a possible legal
order of execution of various subexpressions that results in the
variable 'i' being modified more than once between sequence points.

Is it? I mean, I shudder in horror at that line of code, but I
thought a function call represented a sequence point. Not so?

A function call itself introduces a sequence point. But before the call
can be made, parameters need to be evaluated. They can be evaluated in
any order, and there is no sequence point between evaluation of two
parameters.

Further, in class member invocation of the form expr.method(params),
expr and params are evaluated in arbitrary order, again with no sequence
point.

Thus, in the example above, all three (i++) subexpressions may be
evaluated first with no intervening sequence points, before any calls
are made that may introduce said sequence points.

OK, I think I see the subtlety that I was missing.

what you're saying is that this:

s.assign(data[i++]).append(data[i++]).append(data[i++]);

is compiled as if it had been written:

string::append(
string::append(
string::assign( s, data[i++] ), data[i++], data[i++] ) );

So, even though it looks like three independent functions calls, it really
is three nested function calls.

That, I can believe.

For a continuation of this sort of analysis, see:

http://groups.google.com/group/microsoft.public.vc.language/msg/a5d3b3f8a4e3797c

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: Definition of language constructs
    ... In general, execution involves evaluation, and evaluation ... In /some/ other imperative languages. ... Sequence points are a /particular/ way of expressing execution ...
    (comp.lang.c)
  • Re: sequence points and the execution model
    ... then the order of execution is undefined. ... No sequence point between them. ... requires the result A * B as an operand, so its evaluation (in the abstract ... evaluated before the additive expression A*B+C. ...
    (comp.lang.c)
  • Re: function call?
    ... > There is a sequence point after all the function parameters ... > statement has been completed but before execution of the calling ... The only reference to suspending I can find is in 6.2.4#5, ...
    (comp.lang.c)
  • Re: Must an expression be evaluated before its value is used?
    ... this is not a sequence point. ... The evaluation dependency has a consequence of ... seperate the assignment of 5 to x from the assignment of 11 to x. ... comma operator without first evaluating it's second operand. ...
    (comp.std.c)
  • Re: Order of evaluation.
    ... int add ... Btw - To address what Tom stated about sequence points, ... of evaluation of each full-expression 8). ... execution of any expressions or statements in the function ...
    (alt.comp.lang.learn.c-cpp)