Order of parameter evaluation of function call in GCC

一个人想着一个人 提交于 2021-02-10 06:16:41

问题


When I googled this I always got threads about order of evaluation in general stating order of evaluation is unspecified.

I know the parameter evaluation order is unspecified in C in general.

My question is parameter evaluation order in gcc, left to right or right to left ?

Any links to resources would also be appreciated...

EDIT: Removing ambiguity in the question

Well, I'm talking about the situation when

       foo(1+2,2+3,4+9)

which is first evaluated?

is it 1+2 or 4+9... like wise..

Can we come to a declaration by just compiling this in one gcc compiler....? or is it different across different gcc versions also?


回答1:


If you are really asking foo(f1(), f2(), f3()) - which is more interesting than foo(1+2, 3+4, 5+6), since adding 1+2 and 3+4 won't have effect whether it is done first or last or in a random order.

Now, unfortunately, you can not rely on f1() and f2() and f3() being called in any particular order - as long as each function is called ONCE, it's fine for the order to be any order:

   f1, f2, f3
   f1, f3, f2
   f2, f3, f1
   f2, f1, f3
   f3, f2, f1
   f3, f1, f2

(that covers all the permutations for three parameters).

It is entirely up to the compiler which it "thinks is best".

I wrote some code a long time back, and ran into this particular problem - I had something along the lines of:

char foo(char a, char b)
 ... 
 if (a =! 'x')
   foo(fgetc(f), foo(fgetc(f))); 
 ...

Since I expected the FIRST (left) fgetc() to be called first, and the second fgetc(), I could get the right behaviour. And it worked fine on the school computer. Then I took the code home and tried using it on my home computer. And for some reason it didn't work right. It took me quite some time to figure out that foo() was just being called infinitely, because a was never 'x', which stops the recursion - because 'x' would never appear in the second call.

That was using gcc on both machines, but one was a sparc (school computer) and the one at home was a x86 (386, running OS/2, that's how long ago).

The solution is to break it into several lines:

 char aa = fgetc(f);
 char bb = fgetc(f);
 foo(aa, foo(bb)); 



回答2:


#include <stdio.h>

int f1(void){
    printf("In F1\n");
    return 0;
}

int f2(void){
    printf("In F2\n");
    return 0;
}
int f3(void ){
    printf("In F3\n");
    return 0;
}
void f4(int a,int b,int c){
    printf("In F4\n");
    return;
}


int main(){

f4(f1(),f2(),f3());

  getch();
return 0; 
}



output
----------
In F3
In F2
In F1
In F4


but for 
printf("%d..%d..%d",(f1(),f2(),f3()));

output
---------
In F1
In F2
In F3
0..0..0



回答3:


There is no concept of left-to-right or right-to-left evaluation in C++, which is not to be confused with left-to-right and right-to-left associativity of operators: the expression f1() + f2() + f3() is parsed as (f1() + f2()) + f3() due to left-to-right associativity of operator+, but the function call to f3 may be evaluated first, last, or between f1() or f2() at run time.




回答4:


In C, Parameter passing Mechanism is stack based means it uses the stack (known as: Function Stack frame) to store the parameters and hence the order is Right because Stack is LIFO .that was up to my understandings . Kindly have a look at this link ,it will explain you more ,i hope u may find some thing useful http://zoo.cs.yale.edu/classes/cs427/2012a/resources/Chapter_05.pdf

pg#55 ,5.2.3 Parameter Passing Mechanisms




回答5:


I think you're conflating two concepts: 1) order of parameter evaluation (in a function call), and order of argument pushing. In the first, it is unspecified (implementation-dependent) what order parameters are evaluated before being passed to a function. In the second, it depends on the calling convention for the function, with cdecl pushing arguments from right to left, and almost all other conventions (e.g., pascal) pushing from left to right.




回答6:


// according to my tests: left to right for clang-3.4 and right to left
// for gcc-4.8.2
// but don't write any code relying on that: it would be highly unportable

#include <stdio.h>

int one() {
  printf("1\n");
  fflush(stdout);
  return 1;
}

int two() {
  printf("2\n");
  fflush(stdout);
  return 1;
}

int three() {
  printf("3\n");
  fflush(stdout);
  return 1;
}

void test(int a, int b, int c) {
  return;
}

int main() {
  test(one(), two(), three());
}


来源:https://stackoverflow.com/questions/16923010/order-of-parameter-evaluation-of-function-call-in-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!