在函数调用的参数计算顺序? [英] Order of evaluation of arguments in function calling?

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

问题描述

我学习关于用C未定义行为,我来到指出

I am studying about undefined behavior in C and I came to a statement that states that

有函数参数评估没有特定的顺序

there is no particular order of evaluation of function arguments

但后来怎么样 _cdecl _stdcall 标准调用约定一样,其定义说(在书中)该参数是从右到左进行评估。

but then what about the standard calling conventions like _cdecl and _stdcall, whose definition said (in a book) that arguments are evaluated from right to left.

现在我很困惑与这两个定义之一的,依照UB,各国比其他也就是按照调用约定的定义不同。请证明两个。

Now I am confused with these two definitions one, in accordance of UB, states different than the other which is in accordance of the definition of calling convention. Please justify the two.

推荐答案

Graznarak的回答正确地指出,订单其论据是的评估的是其参数的传送的顺序不同

As Graznarak's answer correctly points out, the order in which arguments are evaluated is distinct from the order in which arguments are passed.

这是典型的ABI仅适用于在参数的通过的顺序,例如这是使用的寄存器和/或顺序参数值压入堆栈。

An ABI typically applies only to the order in which arguments are passed, for example which registers are used and/or the order in which argument values are pushed onto the stack.

什么是C标准说是计算顺序是不确定的。例如(记住,的printf 返回 INT 结果):

What the C standard says is that the order of evaluation is unspecified. For example (remembering that printf returns an int result):

some_func(printf("first\n"), printf("second\n"));

C标准说,两个消息将在部分的顺序(评价不交叉)进行打印,但明确不说选择顺序。它甚至可以改变从一个调用到下一个,不违反C标准。它甚至可以评估的第一个参数,然后评估第二个参数,然后按第二个参数的结果压入堆栈,然后把第一个参数的结果到堆栈中。

the C standard says that the two messages will be printed in some order (evaluation is not interleaved), but explicitly does not say which order is chosen. It can even vary from one call to the next, without violating the C standard. It could even evaluate the first argument, then evaluate the second argument, then push the second argument's result onto the stack, then push the first argument's result onto the stack.

这是ABI可能指定哪个寄存器用于传递两个参数,或确切位置堆栈中的值推,这是与C标准的要求是完全一致的。

An ABI might specify which registers are used to pass the two arguments, or exactly where on the stack the values are pushed, which is entirely consistent with the requirements of the C standard.

不过,即使ABI实际需要的评估的在一个指定的顺序发生(这样,例如,印刷第二个\\ N其次第一\\ n将违反ABI),将仍然与C标准是一致的。

But even if an ABI actually requires the evaluation to occur in a specified order (so that, for example, printing "second\n" followed by "first\n" would violate the ABI) that would still be consistent with the C standard.

什么是C标准说是的 C标准本身的没有定义评价的顺序。一些二级标准仍然是可以这样做。

What the C standard says is that the C standard itself does not define the order of evaluation. Some secondary standard is still free to do so.

顺便说一下,这本身不涉及未定义的行为。有些情况下评估未指定的顺序的可以的导致不确定的行为,例如:

Incidentally, this does not by itself involve undefined behavior. There are cases where the unspecified order of evaluation can lead to undefined behavior, for example:

printf("%d %d\n", i++, i++); /* undefined behavior! */

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

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