问题描述
我参加了一个技术面试前几天,我被问
如何与不同的参数号C编译器implments功能?
它是如何通过在堆栈上?
I attended a technical interview a few days ago, and I was askedHow does the C compiler implments function with Variable number of arguments?How does it pass the on the stack?
有谁知道或explian呢?
Does anyone know or can explian that?
谢谢,
丹
推荐答案
据我所知,用C ...
As far as I know, with C...
-
在调用函数的参数推入堆栈从右到左的顺序。
the caller function pushes arguments onto the stack in right-to-left order.
调用者负责调用的函数执行后从堆栈中移除参数。这大概是precisely因为调用者是保证知道它有多少论据在堆栈中,而被调用的函数可能会是错误的。
the caller is responsible for removing the arguments from the stack after the called function has executed. This is probably precisely because the caller is guaranteed to know how many arguments it put on the stack, while the called function might get it wrong.
P.S: 调用约定通常是特定于实现。我刚才所描述什么是被誉为的cdecl调用约定。与此不同,一般被称为STDCALL,其中被调用函数负责从堆栈中移除它的参数调用约定。正因为如此,它不支持可变长度参数列表。
P.S.: Calling conventions are usually implementation-specific. What I just described is known as the "cdecl" calling convention. Contrast this to a calling convention generally known as "stdcall", where the called function is responsible for removing its arguments from the stack. Because of that, it does not support variable-length argument lists.
P.P.S:由于用户nategoose评论,我没有提到的说法如何变量列表实际上是使用的。例如参见STDARG.H> &LT的了解详情。
P.P.S.: As user nategoose commented, I didn't mention how variable argument lists are actually used. See e.g. the POSIX documentation for the <stdarg.h>
header for more information.
这篇关于如何C编译器实现的功能与可变的参数个数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!