我知道在遵循良好做法时我永远不会达到这样的限度.但是,我需要使用一个带有大量参数的自动生成的函数(我不能用它改变任何东西,我从别人那里接收函数).
那么:我可以在gcc resp中使用的最大参数数量是多少.MinGW的?
我发现了这个关于C++语言规范的内容.而这关于C语言标准的限制.我感兴趣的是"现实世界"限制/实施细节是什么的问题.特别是在gcc和MinGW.
另外:达到这样的限制时,我可以期待什么样的错误消息?当通过extern "C"声明在C++应用程序中使用C代码时,这是否会对"正常"C限制产生任何影响?这里可能适用除参数数量之外的其他限制,例如最大线长度?或者最大堆栈大小?
谢谢!
mch*_*mch 11
C标准5.2.4.1说:
4095 characters in a logical source line
127 parameters in one function definition
127 arguments in one function call
Run Code Online (Sandbox Code Playgroud)
如果你有巨大的结构作为参数,那么堆栈大小(1MB - 8MB)是一个限制.
但所有这些限制都远离所有好的做法.
https://gcc.gnu.org/onlinedocs/gcc-4.3.5/cpp/Implementation-limits.html表示gcc有更高的限制(仅限于可用内存).
在 C 中,有用于多个参数的特殊库 (stdarg.h)。使用这个库,您可以编写如下函数:
int a_function ( int x, ... )
{
va_list a_list;
va_start( a_list, x );
}
Run Code Online (Sandbox Code Playgroud)
我认为没有特别的限制。
以下是如何使用该库的示例:(来源:http ://www.cprogramming.com/tutorial/c/lesson17.html )
#include <stdarg.h>
#include <stdio.h>
/* this function will take the number of values to average
followed by all of the numbers to average */
double average ( int num, ... )
{
va_list arguments;
double sum = 0;
/* Initializing arguments to store all values after num */
va_start ( arguments, num );
/* Sum all the inputs; we still rely on the function caller to tell us how
* many there are */
for ( int x = 0; x < num; x++ )
{
sum += va_arg ( arguments, double );
}
va_end ( arguments ); // Cleans up the list
return sum / num;
}
int main()
{
/* this computes the average of 13.2, 22.3 and 4.5 (3 indicates the number of values to average) */
printf( "%f\n", average ( 3, 12.2, 22.3, 4.5 ) );
/* here it computes the average of the 5 values 3.3, 2.2, 1.1, 5.5 and 3.3
printf( "%f\n", average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) );
}
Run Code Online (Sandbox Code Playgroud)