函数声明中参数的最大数量
我知道函数定义中参数的最小数量是零,但是函数定义中参数的最大数量是多少?我问这个问题只是为了知识和好奇心,并不是我要写一个真正的函数。
I know that minimum number of parameters in function definition is zero, but what is the maximum number of parameters in function definition? I am asking the question just for the sake of knowledge and out of curiosity, not that I am going to write a real function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,实施过程中存在一些限制。您的答案在以下 C++ 标准摘录中的粗体文本中给出。
1. C++ 语言
附件 B - 实现数量
另外,它还说$18.3/6,
Yes, there are limits imposed by the implementation. Your answer is given in the bold text in the following excerpt from the C++ Standard.
1. C++ Language
Annex B - Implementation quantities
Besides, it also says in $18.3/6,
2. C 语言
5.2.4.1 翻译限制
实现应能够翻译并执行至少一个程序
包含以下每一项限制中的至少一个实例:
— 127 个块嵌套层
— 63 个条件包含嵌套级别
— 12 个指针、数组和函数声明符(任意组合)修改一个
声明中的算术、结构、联合或不完整类型
— 完整声明符内带括号声明符的 63 层嵌套
— 完整表达式内带括号表达式的 63 层嵌套
— 内部标识符或宏名称中的 63 个有效初始字符(每个字符
通用字符名称或扩展源字符被视为单个
字符)
— 外部标识符中的 31 个重要初始字符(每个通用字符名称
指定 0000FFFF 或更小的短标识符被视为 6 个字符,每个字符
指定 00010000 或更大的短标识符的通用字符名称是
被认为是 10 个字符,并且每个扩展源字符被认为是相同的
作为相应的通用角色名称的字符数(如果有)
— 一个翻译单元中有 4095 个外部标识符
— 在一个块中声明了 511 个具有块作用域的标识符
— 在一个预处理翻译单元中同时定义 4095 个宏标识符
— 一个函数定义中有 127 个参数
— 一次函数调用中有 127 个参数
— 一个宏定义中有 127 个参数
— 一次宏调用中有 127 个参数
— 逻辑源代码行中有 4095 个字符
— 字符串文字或宽字符串文字中的 4095 个字符(连接后)
— 对象中 65535 字节(仅在托管环境中)
— #included 文件有 15 个嵌套级别
— switch 语句的 1023 个 case 标签(不包括任何嵌套 switch 的标签)
声明)
— 单个结构或联合中有 1023 个成员
— 单个枚举中有 1023 个枚举常量
— 单个 struct-declaration-list 中的 63 层嵌套结构或联合定义
然而,它在注释中说,
2. C Language
5.2.4.1 Translation Limits
The implementation shall be able to translate and execute at least one program that
contains at least one instance of every one of the following limits:
— 127 nesting levels of blocks
— 63 nesting levels of conditional inclusion
— 12 pointer, array, and function declarators (in any combinations) modifying an
arithmetic, structure, union, or incomplete type in a declaration
— 63 nesting levels of parenthesized declarators within a full declarator
— 63 nesting levels of parenthesized expressions within a full expression
— 63 significant initial characters in an internal identifier or a macro name (each
universal character name or extended source character is considered a single
character)
— 31 significant initial characters in an external identifier (each universal character name
specifying a short identifier of 0000FFFF or less is considered 6 characters, each
universal character name specifying a short identifier of 00010000 or more is
considered 10 characters, and each extended source character is considered the same
number of characters as the corresponding universal character name, if any)
— 4095 external identifiers in one translation unit
— 511 identifiers with block scope declared in one block
— 4095 macro identifiers simultaneously defined in one preprocessing translation unit
— 127 parameters in one function definition
— 127 arguments in one function call
— 127 parameters in one macro definition
— 127 arguments in one macro invocation
— 4095 characters in a logical source line
— 4095 characters in a character string literal or wide string literal (after concatenation)
— 65535 bytes in an object (in a hosted environment only)
— 15 nesting levels for #included files
— 1023 case labels for a switch statement (excluding those for any nested switch
statements)
— 1023 members in a single structure or union
— 1023 enumeration constants in a single enumeration
— 63 lev els of nested structure or union definitions in a single struct-declaration-list
However, it says in notes that,
正如之前的回答者已经充分指出的那样,参数的数量取决于 C++ 编译器。编译器限制很可能是由于硬件环境施加的限制。
例如,VAX CPU 架构 使用 8 -bit 字段保存参数数量,因此兼容的 VAX 代码不能传递超过 255 个参数。
同样,许多小型处理器(例如 Intel 8048 系列)的内存非常有限,以至于大多数 C 和 C++ 编译器都给如果使用更多大约四个参数,则会感到悲伤。尽管许多主流开发人员可能会对 8048 嗤之以鼻,但它的后代仍然是部署最广泛的 CPU 之一——主要在嵌入式设备中。
许多 RISC 架构实现指定使用寄存器而不是堆栈的参数传递方法。例如,基本的 ARM CPU 只有 16 个寄存器。参数的最大数量很可能取决于编译器命令行选项,这些选项会影响它生成传递参数的代码的方式。也许超过八个被压入堆栈。也许不是。
如果这些示例看起来过于严格,请考虑 Palm OS 将 C 函数限制为单个参数,并且必须为
int
或void *
类型。As previous answerers have adequately noted, the number of parameters depends on the C++ compiler. The compiler limit may well be due to limitations imposed by hardware environments.
For example the VAX CPU architecture uses an 8-bit field which holds the number of arguments, so compliant VAX code cannot pass more than 255 parameters.
Likewise many small processors like the Intel 8048 family are so constrained for memory that most C and C++ compilers give grief if more about four parameters are used. While many mainstream developers may sneer at the 8048, its descendants remain one of the most widely deployed CPUs—mostly in embedded devices.
Many RISC architecture implementations specify a parameter passing method using registers, rather than the stack. A basic ARM CPU, for example, has only 16 registers. The maximum number of parameters could well depend on compiler command line options which affect how it generates code to pass parameters. Maybe more than eight are pushed onto a stack. Maybe not.
If those examples seem overly restrictive, consider that the Palm OS limited a C function to a single parameter, and that had to be of type
int
orvoid *
.当然,如果您正在用 C 语言编写一个带有如此大量参数的函数,您可能想要重新考虑程序的设计。
Of course, if you're writing a function in C with such a large number of arguments, you probably want to rethink the design of your program.