Re: Order of function evaluation

From: Kevin Goodsell (usenet1.spamfree.fusion_at_neverbox.com)
Date: 10/25/03


Date: Sat, 25 Oct 2003 07:35:26 GMT

Jens.Toerring@physik.fu-berlin.de wrote:
> Hi,
>
> I have a possibly rather stupid question about the order of evaluation
> in a statement like this:
>
> result = foo( x ) - bar( y );
>
> How can I make 100% sure that foo(x) is evaluated before bar(y), where
> foo() and bar() can be either functions or macros?

<snip>

>
> result = foo( x );
> result -= bar( y );
>

Like others said, this should do it... in theory.

Suppose that foo and bar are defined this way:

#define foo(x) ((x) + *p)
#define bar(x) ((x) - *p)

where 'p' is a pointer to some kind of register or memory-mapped I/O
port. In this case, the compiler could "know" that the expression '*p'
doesn't change in those two lines, and could therefore store the result
from the foo call and use it in the bar call. This could give incorrect
results if *p can, in fact, change after it's used in foo, before it's
used in bar.

Cases like these are why we have the 'volatile' qualifier. In the
example above, 'p' should be declared with volatile. The standard
doesn't require that this solves the problem as far as I know, but the
intended purpose of volatile is to tell the compiler to expect the thing
to change in ways it can't predict, so it *should* work.

-Kevin

-- 
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


Relevant Pages

  • Re: m4 Problems
    ... The trick is to avoid evaluation of the two conditional parts by quoting ... them at least ONE level more than the enclosing ifdef(). ... will still evaluate `foo' and `bar' at the outter level, ...
    (freebsd-questions)
  • Re: Order of function evaluation
    ... >with this when writing some hardware-related stuff where foo() and bar() ... >are functions or macros (I want to make sure the solution is independent ... pure functions and without such assumptions the order of evaluation cannot ...
    (comp.lang.c)
  • Re: Beginner post-increment question
    ... So, for example, a compiler could legally translate: ... break it down into several statements, with one evaluation per statement. ... if foo() and bar() each produced output as well as returning ...
    (comp.lang.c)
  • Re: Order of function evaluation
    ... This is because in the second case, there is a sequence ... associated with the evaluation of the assignment expression (i.e. - the ... of foo() and bar(). ...
    (comp.lang.c)
  • Re: Insert with response
    ... FooBar, there's no way and no need to put them in synch. ... column in the foo table to 250 calumns in the bar table. ... set statistics time off ...
    (microsoft.public.sqlserver.programming)