Maximum number of parameters in function declaration

做~自己de王妃 提交于 2019-11-26 18:03:57

问题


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.


回答1:


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

  1. Because computers are finite, C + + implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

  2. The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.
    — Nesting levels of compound statements, iteration control structures, and selection control structures [256].
    — Nesting levels of conditional inclusion [256].
    — Pointer, array, and function declarators (in any combination) modifying an arithmetic, structure, union, or incomplete type in a declaration [256].
    — Nesting levels of parenthesized expressions within a full expression [256].
    — Number of characters in an internal identifier or macro name [1 024].
    — Number of characters in an external identifier [1 024].
    — External identifiers in one translation unit [65 536].
    — Identifiers with block scope declared in one block [1 024].
    — Macro identifiers simultaneously defined in one translation unit [65 536].
    — Parameters in one function definition [256].
    — Arguments in one function call [256].

    — Parameters in one macro definition [256].
    — Arguments in one macro invocation [256].
    — Characters in one logical source line [65 536].
    — Characters in a character string literal or wide string literal (after concatenation) [65 536].
    — Size of an object [262 144].
    — Nesting levels for #include files [256].
    — Case labels for a switch statement (excluding those for any nested switch statements) [16 384].
    — Data members in a single class, structure, or union [16 384].
    — Enumeration constants in a single enumeration [4 096].
    — Levels of nested class, structure, or union definitions in a single struct-declaration-list [256].
    — Functions registered by atexit()[32].
    — Direct and indirect base classes [16 384].
    — Direct base classes for a single class [1024].
    — Members declared in a single class [4 096].
    — Final overriding virtual functions in a class, accessible or not [16 384].
    — Direct and indirect virtual bases of a class [1 024].
    — Static members of a class [1 024].
    — Friend declarations in a class [4 096].
    — Access control declarations in a class [4 096].
    — Member initializers in a constructor definition [6 144].
    — Scope qualifications of one identifier [256].
    — Nested external specifications [1 024].
    — Template arguments in a template declaration [1 024].
    — Recursively nested template instantiations [17].
    — Handlers per try block [256].
    — Throw specifications on a single function declaration [256].

Besides, it also says in $18.3/6,

Implementation Limits: The implementation shall support the registration of at least 32 functions.




回答2:


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,

13) Implementations should avoid imposing fixed translation limits whenever possible.




回答3:


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 or void *.




回答4:


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.



来源:https://stackoverflow.com/questions/4582012/maximum-number-of-parameters-in-function-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!