argument evaluation

A

aarklon

Hi all,

what is the standard way for evaluating arguments in a C function is
it Right to left evaluation or right to left evaluation ???
 
J

Joachim Schmitz

Hi all,

what is the standard way for evaluating arguments in a C function is
it Right to left evaluation or right to left evaluation ???
It is undefined behavoir, so the implementation if free to it in any order.

Bye, Jojo
 
G

gallows

Hi all,

what is the standard way for evaluating arguments in a C function is
it Right to left evaluation or right to left evaluation ???

I think there is no indication in the standard. For instance:

func( foo(), bar() );

You can't know if foo() will be executed before or after bar().

s.
 
R

Richard Heathfield

Joachim Schmitz said:
It is undefined behavoir, so the implementation if free to it in any
order.

ITYM "unspecified behaviour". If it were undefined, the implications would
be astronomical, and all bad.
 
F

Flash Gordon

Joachim Schmitz wrote, On 04/11/07 12:53:
It is undefined behavoir, so the implementation if free to it in any order.

Undefined behaviour has a specific meaning, and this is not it. The
order is unspecified.
 
E

Eric Sosman

Hi all,

what is the standard way for evaluating arguments in a C function is
it Right to left evaluation or right to left evaluation ???

(You don't seem to be offering a wide range of choices ...)

The argument expressions in a C function call are evaluated
counter-clockwise.
 
J

Joachim Schmitz

Richard Heathfield said:
Joachim Schmitz said:


ITYM "unspecified behaviour". If it were undefined, the implications would
be astronomical, and all bad.
Yes, you're right, of course.
6.5.2.2-10 in n1256.pdf
10 The order of evaluation of the function designator, the actual arguments,
and subexpressions within the actual arguments is unspecified, but there is
a sequence point before the actual call.
 
A

aarklon

Joachim Schmitz wrote, On 04/11/07 12:53:


Undefined behaviour has a specific meaning, and this is not it. The
order is unspecified.

then what about gcc compiler, what order is it following??
 
F

Flash Gordon

then what about gcc compiler, what order is it following??

Whatever it feels like on the particular system it is compiling for with
the optimisation level selected. It is not even required to document
what that order is or keep it consistent.
 
E

Eric Sosman

Flash said:
Whatever it feels like on the particular system it is compiling for with
the optimisation level selected. It is not even required to document
what that order is or keep it consistent.

For example, in a function call like

f(a*x+y, a*x+z);

.... the optimizer may very well arrange to evaluate the common
sub-expression just once, producing something that might be
written as

t = a*x;
f(t+y, t+z);

If so, *neither* argument expression is evaluated "before" or
"after" the other; their evaluations are intermixed.
 
K

Keith Thompson

then what about gcc compiler, what order is it following??

Why do you care? You should never write code whose behavior depends
on the order in which arguments are evaluated. Such code is likely to
"work" on the system for which you write it, and fail (subtly or
catastrophically) when you try to compile it on some other system, or
on a newer version of the same compiler, or on the same compiler with
different otimization options, or during a full moon.

The compiler is free to rearrange code between consecutive sequence
points. Since there are no sequence points between argument
evaluations in a single function call, the compiler is free to
rearrange the argument evaluations in any way it likes. If you need a
specific order of evaluation, you need to introduce sequence points.
For example, this:

foo(expr_with_side_effects, expr_with_more_side_effects);

is dangerous, but this:

tmp1 = expr_with_side_effects;
tmp2 = expr_with_more_side_effects;
foo(tmp1, tmp2);

is likely to be safe (if you haven't made any other errors). And
since you're going to use more meaningful names that "tmp1" and
"tmp2", it's likely to be easier to read and maintain.

If that doesn't solve your problem, tell us what you're trying to
accomplish, and we can probably help.
 
C

Chris Dollin

what is the standard way for evaluating arguments in a C function is
it Right to left evaluation or right to left evaluation ???

No. The Standard way is "any order the compiler likes"; as the programmer,
this means that you must write your calls so that this variability /does
not matter/.

Whatever the arguments against this choice of ordering -- and there are
such arguments, as well as arguments in favour -- don't matter; that's
what you're given.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top