1

Suppose I have a function like this:

int *calculator(int *pointer1, int *pointer2) {
    int *pointer3 = malloc(15);
    return pointer3; //not an actual return, just an example function
}

and then in main, I have multiple calls to it:

int main(void) {
    int array[40];
    int *ptr, *ptr2, *ptr3, *ptr4;
    ptr = ptr2 = ptr3 = ptr4 = array; // please don't mind this, i don't  actually write code this way.
    int *answer = calculator(calculator(ptr, ptr2), calculator(ptr3,calculator(ptr4, ptr5));
    free(answer);
    return 0;
}

Is the order of function calls from right to left? Will the last call to calculator(ptr4, ptr5) be the first function to be calculated and returned?

Mephy
  • 2,978
  • 3
  • 25
  • 31
Spartan 117
  • 520
  • 2
  • 6
  • 21
  • Try it an see! Make `calculator()` take a number as a parameter and make each call pass a different number. See what order they come out. – John3136 Sep 10 '15 at 00:30
  • 1
    possible duplicate of [Parameter evaluation order before a function calling in C](http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c) – Jim Lewis Sep 10 '15 at 00:34
  • Note your "example function" invokes undefined behaviour by allocating memory for an `int []` with wrong size (unless you do not use the extra 1..n bytes). – too honest for this site Sep 10 '15 at 00:35
  • @John3136 - the problem with that is that it doesnt give the answer. Maybe its undefined behavior on on your machine, today its left to right, maybe tomorrow its right to left or if you add another 2 params its l to r. Or it could be that the C specs say its implementation defined, and so you have to read the manual on the compiler ('on big endian we do l to r etc'). Or it could be that the C spec says 'always l to r' – pm100 Sep 10 '15 at 00:46

3 Answers3

4

What you are asking depends on the effective invocation tree, in your case you have

0 calculator
    1 calculator
        ptr
        ptr2
    2 calculator
        ptr3
        3 calculator
          ptr4
          ptr5

In C you don't have guarantees about the order of evaluation of arguments of a function call, but there is a sequence point before entering the effective function call.

This means that you are sure that all side effects of the arguments are fully resolved before calling the function itself.

So summarizing:

  • 3 calculator must be called before 2 calculator
  • to call 0 calculator both 1 calculator and 2 calculator must be called but their order is unspecified (can be 1,2 or 2,1)

Please check this answer for further explanation.

Community
  • 1
  • 1
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Thank you for the detailed explanation! It cleared up my confusion in a clear and concise manner. So is it safe to assume that if a function call is an argument to another function, the argument function call will be evaluated before the outer function correct? – Spartan 117 Sep 10 '15 at 00:46
  • The order could be any of: 3210 3120 1320 – M.M Sep 10 '15 at 01:16
0

The order of function calls is actually not guaranteed if they are parameters.
There is a guarantee that all affects of the functions in the parameters will be applied before the base function is called.

Below is a quote from the C Wiki Order of Evaluation page for order of function parameter resolution:

Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified (except where noted below). The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.

Serdalis
  • 10,296
  • 2
  • 38
  • 58
0

It depends on the compiler. One compiler may evaluate arguments from left to right. Another compiler may evaluate right to left. There is no fixed rule about this.

Jobin Jose
  • 184
  • 1
  • 2
  • 14