Re: Order of function evaluation
From: Kevin Goodsell (usenet1.spamfree.fusion_at_neverbox.com)
Date: 10/25/03
- Next message: rihad: "Re: reset char array"
- Previous message: Micah Cowan: "Re: wait until \n appears in string"
- In reply to: Jens.Toerring_at_physik.fu-berlin.de: "Order of function evaluation"
- Next in thread: Jens.Toerring_at_physik.fu-berlin.de: "Re: Order of function evaluation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: rihad: "Re: reset char array"
- Previous message: Micah Cowan: "Re: wait until \n appears in string"
- In reply to: Jens.Toerring_at_physik.fu-berlin.de: "Order of function evaluation"
- Next in thread: Jens.Toerring_at_physik.fu-berlin.de: "Re: Order of function evaluation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|