Re: Benefit of not defining the order of execution



Keith Thompson wrote:
Nate Eldredge <nate@xxxxxxxxxx> writes:
cri@xxxxxxxx (Richard Harter) writes:
(Think regression tests on log files.)

Huh?

Consider a program containing something like this:

double result = this() * that() + the_other();

Assuming that the three functions don't interact in any nasty ways
(depending on the same global variables, for example), the order of
evaluation shouldn't matter; result will get the same value for any of
the 6 possible orders. But suppose the functions contain trace
statements that (perhaps optionally) log information to a file. Then
the contents of the log can vary depending on the evaluation order,
and a testing scheme that looks for possible regressions by detecting
changes in the contents of the log will have problems.

Since the order of evaluation and thus the order of the log statements (side effects that _do_ interact in a "nasty" way) is unspecified, your testing scheme is broken if it assumes that your log statements are only correct when in a certain order.

If you want them to appear in a particular order, make proper use of sequence points to enforce that order. If you tell the compiler that the order doesn't matter, which is what you're doing when you write code such as above, then you shouldn't be surprised when it takes advantage of that.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Isaac Jaffe
.



Relevant Pages

  • Re: Benefit of not defining the order of execution
    ... left-to-right evaluation order, ... they don't interact (by which I mean, ... There is one other way in which undefined order of evaluation ... Unspecified OoE often is the cause of undefined behavior. ...
    (comp.lang.c)
  • Re: Benefit of not defining the order of execution
    ... Even if a clever optimizing compiler could recover (relative to C as ... left-to-right evaluation order, ... they don't interact (by which I mean, ... There is one other way in which undefined order of evaluation ...
    (comp.lang.c)
  • Re: Benefit of not defining the order of execution
    ... they don't interact (by which I mean, ... to force a particular order of evaluation. ... Which underscores the point that as soon as evaluation order is ... distant future I can see an optimizer deciding that fand gcan be evaluated ...
    (comp.lang.c)
  • Re: Benefit of not defining the order of execution
    ... there is no reason why that order has to be ... Apparently several people are of the opinion that having language ... defined left-to-right order of evaluation (and it can't, ... they don't interact (by which I mean, ...
    (comp.lang.c)
  • Re: Benefit of not defining the order of execution
    ... they don't interact (by which I mean, ... to force a particular order of evaluation. ... As others have pointed out you assume error-free programmers ... imposing left-to-right evaluation order would be at least as bad ...
    (comp.lang.c)