你还能用旧的方式在 C 中处理变量参数吗?
纯粹出于兴趣,我在……我不知道……15 - 20年后重新学习C。
我似乎记得变量参数是作为简单的宏实现的。
- 有人能记得他们是什么吗?
编辑:为了澄清我的问题,我知道它们与 va_list 等具有相同的名称,但你记得实际的宏定义吗?
- 他们今天还会工作吗?
Purely for interest, I am re-learning C after ... I don't know ... 15 - 20 years.
I seem to recall that variable arguments were implemented as simple macros.
- Can anyone remember what they were?
Edit: To clarify my question, I know they had the same name as va_list etc. but can you remember the actual macro definition?
- Will they still work today?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在考虑 stdarg.h (WP 文章链接)。他们仍然工作得很好。 :-)
You're thinking of
va_list
,va_arg
,va_start
, andva_end
from stdarg.h (link to WP article). They still work just fine. :-)甚至在标准化之前,进行变量参数的方法就很常见。 C89标准给出了一个基于现有实践的标准化接口。这些实践的其余部分仍然存在,例如
仍然存在于 Unix98 中:/varargs.h.html" rel="nofollow noreferrer">http://www.opengroup.org/onlinepubs/007908799/xsh/varargs.h.html (但在当前版本 宏的实现始终非常依赖于系统(堆栈在两个方向上增长,甚至有使用链表作为堆栈的系统,堆栈帧中各种内容的位置取决于处理器和通用约定,在某些处理器上 - - Sparc 说 - 首先需要保存寄存器,对齐要求可能会导致问题,...)
如果你想知道一个简单的实现是什么样子,这里有一个,取决于假设可能是错误的(它们不尝试让任何对齐正确),并且即使假设成立,也肯定会失败一些极端情况:
Even before the standardisation, methods of doing variable arguments were common. The C89 standard gave a ... standardized interface based on the existing practices. Remainders of those practice still exist at place, for instance
<varargs.h>
was still present in Unix98: http://www.opengroup.org/onlinepubs/007908799/xsh/varargs.h.html (but is no more present in current version)The implementation of the macros was always very much system dependent (there are stacks growing in both directions, there are even systems using a linked list as stack, position of various things in the stack frame depend on the processor and common convention, on some processors -- says Sparc -- one need first to save the registers, alignment requirements may cause problems,...)
If you want to know what would a simple implementation looks like, here is one, depending on assumptions probably false (they don't try to get any alignment right) and also certainly failing some corner cases even when the assumptions hold:
可变参数函数确实通常使用宏来实现。这些都是标准化的 - 请参阅有关该主题的这篇维基百科文章。实际的宏定义取决于处理器架构,因为它们必须使用调用堆栈来玩非便携式游戏。
Variadic functions are indeed commonly implemented using macros. These are standardised - see this wikipedia article on the subject. The actual macro definitions are dependent on the processor architecture, as they have to play non-portable games with the call stack.
今天的变量参数是通过
- 至于15年前他们是怎么做的,我实在说不上来。Variable arguments today are done via the interface described in
<stdarg.h>
- as to how they were done 15 years ago, I really can't say.很难说你从很久以前开始实施它们是否仍然有效,但是它们与其他人指出的几乎相同,它们只是进入了标准标题(标准是操作词)。
编译器作为内置扩展的内容取决于编译器以及调用它时传递给它的标志。
请记住,gcc 并不是唯一的 C 编译器,但它(与其他编译器一样)符合 c89 和 c99。
Its hard to say if your implementation of them from way back when would still work, however they are pretty much the same as others have pointed out, they just moved into a standard header (standard being the operative word).
What a compiler expands as a built in is up to the compiler, and the flags that you pass to it when invoking it.
Keep in mind that gcc is not the only C compiler, but it (like others) conforms to c89 and c99.