Nested function calls order of evaluation [duplicate]
Asked Answered
S

4

10

It's well-known that the order of evaluation of a function's arguments in unspecified and can differ between different compilers.

What doesn't seem so clear is whether function calls can be interleaved, in the following sense:

f(g(h()), i(j()))

Let's assume the compiler chooses to evaluate f's first parameter first. Is the compiler free to call j between calling h and g? I believe so, but I don't know where to find confirmation in the Standard.

Sixtieth answered 7/5, 2009 at 10:27 Comment(3)
See this.Hazing
It also applies to plain C, or is the standard any different ? (that would be quite interesting)Geotropism
The C Standard says "The order of evaluation of the function designator, the arguments, and subexpressions within the arguments is unspecified, but there is a sequence point before the actual call.", so the effect seems to be the same.Sixtieth
W
14

The evaluation order is unspecified - see section 5.2.2/8 of the Standard:

The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations take effect before the function is entered.

Whitley answered 7/5, 2009 at 10:39 Comment(2)
+1: it contains the answer to your "Is the compiler free to call j between calling h and g?" question that is "Yes".Geotropism
I'm not so sure if you can derive that answer from the quote above. In general, while there is considerable lattitude to the order of evaluation of arguments, I do not think that you can do anything but call a function after you've finished evaluating its arguments.Carmelcarmela
C
4

I don't know what the standard says, but I think that if it matters to you, then you should probably re-arrange the code so that you know what's going to happen in which order (temp variables are your friends). Even if you figure out what the standard says, AND if we assume that your compiler implements that correctly, you're still leaving a maintenance time bomb, because your successors WON'T know everything you do.

Cowes answered 7/5, 2009 at 10:35 Comment(1)
Fair enough. I can't think of any code twisted enough that it would actually matter :-)Sixtieth
A
2

The evaluation order is not specified by the standart. It depends only on your compiler.

Adiabatic answered 7/5, 2009 at 10:33 Comment(1)
In this case it depends on your compiler and potentially many other things. The freedom is given to allow optimizers to do the best job possible. As a result a compiler might produce a different order depending on the optimisation level.Browse
B
0

If the functions you're using in the same expression are somehow related (one affects the results of the other), so different order of calls give different results, then refrain of using nested functions in expressions.

I do it as good practice, exactly because, as the other said, the calling order is undefined (you could have even interlaced execution, if the optimizer thought it will be more optimal).

Babirusa answered 7/5, 2009 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.