功能参数的评估顺序 [英] Evaluation order of function parameters

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

问题描述

考虑这个功能,


void fun(int i,int j)

{

printf(" i =%d:j =%d",i,j);

}


我称之为:


fun(n ++,n);


这在用gcc 4.0编译时给出了以下输出:

i = 9:j = 10


但是在gcc 3.4.3上输出的是:

i = 9:j = 9


虽然订单对函数参数的评估是未指定的

编译器在不同的版本中是否仍然不一致?

可以解释为什么他们在版本4中更改了gcc?


Rgrds,

haroon

Consider this function,

void fun (int i, int j)
{
printf ("i = %d : j = %d", i, j);
}

I call it like this:

fun (n++, n);

this when compiled with gcc 4.0 gives following out put:
i = 9 : j = 10

but on gcc 3.4.3 the out put is:
i = 9 : j = 9

although the order of evaluation for function arguments is unspecified
still shouldn''t the compiler be consistent in its different versions?
can any one explain why did they change this in version 4 of gcc?

Rgrds,
haroon

推荐答案

haroon写道:
考虑这个函数,

void fun(int i,int j)
{/> printf(" i =%d: j =%d,i,j);
}
我称之为:

有趣(n ++,n);
< br / >这个用gcc 4.0编译时给出了以下输出:
i = 9:j = 10

但是在gcc 3.4.3上输出的是:
i = 9: j = 9

虽然函数参数的评估顺序是未指定的,但编译器的不同版本仍然不应该一致吗?
可以解释为什么他们为什么在gcc的第4版中更改此内容?
Consider this function,

void fun (int i, int j)
{
printf ("i = %d : j = %d", i, j);
}

I call it like this:

fun (n++, n);

this when compiled with gcc 4.0 gives following out put:
i = 9 : j = 10

but on gcc 3.4.3 the out put is:
i = 9 : j = 9

although the order of evaluation for function arguments is unspecified
still shouldn''t the compiler be consistent in its different versions?
can any one explain why did they change this in version 4 of gcc?




您的代码调用未定义的行为,因此编译器可以随意执行

。为什么不同版本之间应该保持一致,甚至在相同版本的不同编辑中使用




Your code invokes undefined behaviour, so the compiler is allowed to do
as it pleases. Why should it be consistent across versions, or even
across different compilations with the same version?


" haroon" <公顷*********** @ gmail.com>写道:
"haroon" <ha***********@gmail.com> wrote:
虽然函数参数的评估顺序是未指定的
仍然不应该编译器在不同的版本中保持一致吗?
任何人都可以解释为什么他们在gcc的第4版中更改了这个?
although the order of evaluation for function arguments is unspecified
still shouldn''t the compiler be consistent in its different versions?
can any one explain why did they change this in version 4 of gcc?




< http://www.eskimo.com/~scs/C-faq/q3.2。 html> ;.


HTH; HAND。


Richard



<http://www.eskimo.com/~scs/C-faq/q3.2.html>.

HTH; HAND.

Richard


haroon,我同意你的看法。

至少有一个希望来自同一编译器的不同版本的类似行为。


ya这是正确的......但是,一个shud永远不会在第一个编写这样的代码

地方。

一个人永远不能争辩..因为C语言没有指定这种函数调用的行为

。它始终是特定于编译器的。


bt我想知道可能是编译器设计者

在改变行为时的想法。我认为这是一个优化问题。
问题。这个问题的任何接受者???


问候,

aakash

haroon写道:
well haroon, i agree with you.
atleast one shud expect similar kind of behavior from the different
versions of the same compiler.

ya thats right ... tht, one shud never write such code in the first
place.
one can never argue .. as the C language doesnt specifies the behavior
of such function calls. it is always compiler specific.

bt i would like to know that wht might be the compiler designer
thinking while changing the behavior. i dnt think its an optimization
issue. any takers for this question ???

regards,
aakash
haroon wrote:
考虑这个函数,

void fun(int i,int j)
{/> printf(" i =%d:j =%d",i,j);
}

我称之为:

有趣(n ++,n);

这在用gcc 4.0编译时给出了以下内容out put:
i = 9:j = 10

但是在gcc 3.4.3上输出的是:
i = 9:j = 9
任何人都可以解释为什么他们在gcc的第4版中改变了这一点? br />
Rgrds,
haroon
Consider this function,

void fun (int i, int j)
{
printf ("i = %d : j = %d", i, j);
}

I call it like this:

fun (n++, n);

this when compiled with gcc 4.0 gives following out put:
i = 9 : j = 10

but on gcc 3.4.3 the out put is:
i = 9 : j = 9

although the order of evaluation for function arguments is unspecified
still shouldn''t the compiler be consistent in its different versions?
can any one explain why did they change this in version 4 of gcc?

Rgrds,
haroon






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

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