[comp.sys.amiga.programmer] Side Effects == Rattlesnakes -- watch out for them

xanthian@zorch.SF-Bay.ORG (Kent Paul Dolan) (05/04/91)

farren@well.sf.ca.us (Mike Farren) writes:

> jsmoller@jsmami.UUCP (Jesper Steen Moller) writes:

>>    result=func1()+func2();

>> Question: Can't you rely on func1() being called before func2()???
>> Really not?

> Nope. Really not. There is no guarantee at all that the compiler will
> call func1 first - the only guarantee is that both functions will have
> been called before the add takes place :-)

>> Oh, by the way: func(a++) Is "a" increased before or after the
>> function call. I think I once read in K&R that it was undefined, but
>> I might be mistaken.

> Nope, not undefined. a is evaluated, and passed to the function when
> it is called, and then is incremented. This *is* guaranteed.

Which still leaves the wide open "gotcha" that if func() modifies "a"
by a side effect during execution, all bets are off as to whether the
pre-call or the post-call value is what gets incremented and stored
back in "a".

Best rule for C side effects -- don't.

Kent, the man from xanth.
<xanthian@Zorch.SF-Bay.ORG> <xanthian@well.sf.ca.us>

andrew@teslab.lab.OZ (Andrew Phillips) (05/15/91)

>>> jsmoller@jsmami.UUCP (Jesper Steen Moller) writes:
>>>> Oh, by the way: func(a++) Is "a" increased before or after the
>>>> function call. I think I once read in K&R that it was undefined, but
>>>> I might be mistaken.

You are mistaken.  You are probably confusing this with the fact that
the it is not specified whether the function designator is evaluated
before or after the arguments.  (Note that the function designator
may have side effects as it can be any expression that evaluates to a
pointer to a function.)

From K&R2 page 202:
"The order of evaluation of arguments is unspecified; ...  However,
the arguments and the function designator are completely evaluated,
including all side effects, before the function is entered."

I apologise for the delay in adding to this thread (I have been away)
but this fact made the whole discussion rather absurd.

Andrew.
-- 
Andrew Phillips (andrew@teslab.lab.oz.au) Phone +61 (Aust) 2 (Sydney) 289 8712