GCC中函数调用的参数评估顺序 [英] Order of parameter evaluation of function call in GCC

查看:87
本文介绍了GCC中函数调用的参数评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用谷歌搜索这个时,我总是得到关于评估顺序的线索,一般来说,评估顺序是未指定的.

我知道在 C 中一般未指定参数评估顺序.

我的问题是gcc中的参数求值顺序,是从左到右还是从右到左?

任何资源链接也将不胜感激...

消除问题中的歧义

好吧,我说的是情况

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

首先评估哪个?

1+2 还是 4+9...

我们可以通过在一个 gcc 编译器中编译它来获得声明吗....?还是在不同的 gcc 版本中也有所不同?

解决方案

如果你真的要问foo(f1(), f2(), f3()) - 这比foo(1+2, 3+4, 5+6),因为1+2和3+4的加法不管是先做还是最后做,还是随机顺序都没有影响.

现在,不幸的是,您不能依赖以任何特定顺序调用的 f1()f2()f3()- 只要每个函数都被调用一次,顺序可以是任意顺序:

 f1, f2, f3f1、f3、f2f2, f3, f1f2, f1, f3f3, f2, f1f3, f1, f2

(涵盖三个参数的所有排列).

这完全取决于它认为最好"的编译器.

我很久以前写了一些代码,遇到了这个特殊的问题 - 我有一些类似的东西:

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

因为我希望首先调用第一个(左)fgetc(),然后调用第二个 fgetc(),所以我可以得到正确的行为.它在学校电脑上运行良好.然后我将代码带回家并尝试在我的家用计算机上使用它.并且由于某种原因它没有正常工作.我花了很长时间才发现 foo() 只是被无限调用,因为 a 从来都不是 'x',这会停止递归 - 因为 'x' 永远不会出现在第二次调用中.

那是在两台机器上都使用 gcc,但其中一台是 sparc(学校计算机),而家里的一台是 x86(386,运行 OS/2,那是很久以前的事了).

解决办法是把它分成几行:

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

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?

解决方案

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)); 

这篇关于GCC中函数调用的参数评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆