功能参数评估顺序 [英] Order of function parameters evaluation

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

问题描述

标准对此有何评价:


#include< stdio.h>

void somefunc(int a,int b,int c )

{

printf("%d%d%d \ n",a,b,c);

}


int main(void){

int i = 5;

somefunc(i ++,i ++,i ++);

返回0;

}


使用gcc -Wall编译它给出:


prog .c:在函数main中:

prog.c:10:警告:对i的操作可能未定义

prog.c:10:警告:'i''上的操作可能是未定义的


但是无论如何它打印7 6 5.


是否保证评估函数参数从右到左或

这是标准没有规定的(上面的代码产生了

UB)?


谢谢

What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}

Compiling with gcc -Wall it gives:

prog.c: In function `main'':
prog.c:10: warning: operation on `i'' may be undefined
prog.c:10: warning: operation on `i'' may be undefined

but it prints 7 6 5 anyway.

Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?

Thanks

推荐答案



子网写道:


subnet wrote:
标准说什么关于这个:

#include< stdio.h>
void somefunc(int a,int b,int c)
{/> printf("%d%d%d \ n",a,b,c);
}
int main(void){
int i = 5;
somefunc(i ++,i ++, i ++);
返回0;
}

用gcc -Wall编译它给出:

prog.c:在函数`main''中:
prog.c:10:警告:对i的操作可能未定义
prog.c:10:警告:对i的操作可能未定义

但无论如何它打印7 6 5.

是否保证从右到左评估函数参数或
这不是由标准指定的(并且上面的代码产生了
UB)?
What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}

Compiling with gcc -Wall it gives:

prog.c: In function `main'':
prog.c:10: warning: operation on `i'' may be undefined
prog.c:10: warning: operation on `i'' may be undefined

but it prints 7 6 5 anyway.

Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?




阅读常见问题解答会很有帮助。

它位于:
http://www.eskimo.com/~scs/C-faq/top.html


从问题3.2开始
http://www.eskimo.com/~scs/C-faq/q3.2.html


-

Al Bowers

坦帕,美国佛罗里达州

mailto: xa ****** @ myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822/



Reading the faq will be helpful.
Its located at:
http://www.eskimo.com/~scs/C-faq/top.html

Start with question 3.2 at
http://www.eskimo.com/~scs/C-faq/q3.2.html

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


subnet写道:
标准对此有何评价:

#include< stdio.h>
void somefunc(int a ,int b,int c)
{/> printf("%d%d%d \ n",a,b,c);
}
int main(void){
int i = 5;
somefunc(i ++,i ++,i ++);
返回0;
}


它说的是上次有人问这个问题时的情况。

;)


函数参数的评估顺序是未指定的。

因为代码修改了一个在没有

(保证)干预序列点的情况下不止一次变量,行为是

undefined。

用gcc -Wall编译它给出:

prog.c:在函数main中:
prog.c:10:警告:对i的操作可能未定义
prog.c:10:警告:对i的操作可能是未定义的

但无论如何它都打印7 6 5。


未定义的行为让一个实现可以做任何事情,包括执行代码和产生输出

这似乎是有效的。

函数参数是否保证正确评估
-to-left或标准未指定(上面的代码生成UB)?
What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}
It says the same thing as the last time someone asked this question.
;)

The order of evaluation of function arguments is unspecified.
Since the code modifies a variable more than once without a
(guaranteed) intervening sequence point, the behaviour is
undefined.
Compiling with gcc -Wall it gives:

prog.c: In function `main'':
prog.c:10: warning: operation on `i'' may be undefined
prog.c:10: warning: operation on `i'' may be undefined

but it prints 7 6 5 anyway.
Undefined behaviour lets an implementation do just about
anything, including executing the code and producing output
which seems valid.
Are the function arguments guaranteed to be evaluated right
-to-left or this is not specified by the standard (and the
above code produces UB)?




指定为未指定。这意味着一个

的实现可以自由选择它任意订购它b / b
喜欢,包括从中到外(无论这意味着什么),

或其他任何看起来最简单的计算优先。


-

彼得



It is specified as being ''unspecified''. This means that an
implementation is free to choose any arbitrary ordering it
likes, including middle-to-out (whatever that might mean,)
or whatever-looks-easiest-to-compute-first.

--
Peter


su****@katamail.com (子网)写道:
su****@katamail.com (subnet) wrote:
标准对此有何评价:
somefunc(i ++,i ++,i ++);


它调用未定义的行为。

使用gcc -Wall编译它给出:

prog.c:在函数`main中'':
prog.c:10:警告:对'i''的操作可能是未定义的


这是正确的。

但是它无论如何打印7 6 5。


未定义的行为可以做任何事情,包括你认为它应该做的事情,但是,更加阴险的是,你这样做了

机器并且在你的主管(下一代)计算机上崩溃。

是否保证从右到左评估函数参数或
这不是指定的按标准(和上面的代码生成
UB)?
What does the standard say about this:
somefunc(i++, i++, i++);
That it invokes undefined behaviour.
Compiling with gcc -Wall it gives:

prog.c: In function `main'':
prog.c:10: warning: operation on `i'' may be undefined
Which is correct.
but it prints 7 6 5 anyway.
Undefined behaviour is allowed to do anything, including what you
thought it should do, but also, more insidiously, doing so on your
machine and crashing on your supervisor''s (next-generation) computer.
Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?




代码产生未定义的行为,但不是(只)因为订单

的评估未指定。它是_is_未指定,但如果那都是

你只是不知道它将以什么顺序打印5,6和7.

但是,那是'不是全部。你修改我三次没有干预

序列点。这意味着你调用了完整的未定义的

行为,并允许该程序在其功能范围内执行任何操作,从无需工作到深入工作

混淆并打印Wibble!。或者更糟。所以不要这样做。


Richard



The code produces undefined behaviour, but not (just) because the order
of evaluation is unspecified. It _is_ unspecified, but if that were all
you merely wouldn''t know in what order it would print 5, 6 and 7.
However, that''s not all. You modify i three times without an intervening
sequence point. This means that you invoke full-blown undefined
behaviour, and the program is allowed to do literally anything within
its powers, ranging from nothing to appearing to work to getting deeply
confused and printing "Wibble!". Or worse. So don''t do it.

Richard


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

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