是否可以验证可变参数计数? [英] Is it possible to validate a variable argument count?

查看:31
本文介绍了是否可以验证可变参数计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 中,在 stdarg.h 库下,函数或过程可以接受变量参数,并且通常参数列表前面是要跟随的参数数量的计数.是否可以验证用户的参数计数?

In C, under the stdarg.h library, variable arguments can be accepted by a function or procedure and usually the argument list is preceded by a count of the number of arguments to follow. Is it possible to validate the user's count of the arguments?

我怀疑这一定是可能的,因为 printf() 是一个可变参数函数,如果程序员在格式字符串中的标签数量与提供的参数数量之间存在不匹配,则会发生运行时错误.因此,似乎 printf 有某种方法可以检测提供的参数数量.

I suspect that it must be possible, because printf() is a varargs function and if the programmer has a mismatch between the number of tags in the format string, and the number of arguments supplied, then a runtime error occurs. So, it seems that printf has some way of detecting the number of arguments supplied.

推荐答案

C 标准没有为此提供任何便利.通常,printf 实现不能也不会为此1 提供运行时错误,尽管使用不正确的参数会产生运行时错误,例如尝试访问未映射的内存.C 编译器通常会为此提供编译时错误,当它们可以看到格式字符串时.

The C standard does not provide any facility for this. In general, printf implementations cannot and do not provide run-time errors for this1, although using incorrect arguments can generate run-time errors such as attempts to access unmapped memory. C compilers commonly provide compile-time errors for this, when they can see the format string.

1 C 实现提供此功能在技术上并非不可能,但大多数 C 实现不具备该功能,主要是因为它们符合不支持它的 ABI.

1 It is not technically impossible for a C implementation to provide this as a feature, but most C implementations do not have the capability, largely because they conform to ABIs that do not support it.

这篇关于是否可以验证可变参数计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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