I know that I would never hit such a limit when following good practices. However, I need to use an automatically generated function with lots of parameters (and I cannot change anything with it, I receive the function from someone else).
So: What is the maximum number of parameters I can use in gcc resp. MinGW?
I've found this about the C++ language specification. And this about the C language standard limits. What I am interested in is the question what the "real world" limits / implementation details are. Especially in gcc and MinGW.
Also: What kind of error message could I expect when hitting such a limit? And when using C Code in a C++ application via the extern "C"
declaration, does this make any difference to the "normal" C limit? Might other limits than the number of parameters apply here, e.g. something like a maximum line length? Or the maximum stack size?
Thanks!
The main function can be defined with no parameters or with two parameters (for passing command-line arguments to a program when it begins executing). The two parameters are referred to here as argc and argv, though any names can be used because they are local to the function in which they are declared.
The maximum number of parameters is limited to 126 by functions.
Neither the C nor C++ standard places an absolute requirement on the number of arguments/parameters you must be able to pass when calling a function, but the C standard suggests that an implementation should support at least 127 parameters/arguments (§5.2.
the C Standard 5.2.4.1 says:
4095 characters in a logical source line
127 parameters in one function definition
127 arguments in one function call
also the stack size (1MB - 8MB) is a limit, if you have huge structs as arguments.
But all these limits are far away from all what's good practice.
https://gcc.gnu.org/onlinedocs/gcc-4.3.5/cpp/Implementation-limits.html says that gcc has much higher limits (limited only by available memory).
In C there are special librarys (stdarg.h) for multiple parameters. With this library you can write functions like this:
int a_function ( int x, ... )
{
va_list a_list;
va_start( a_list, x );
}
And i don't think there is a particular limit.
Here is a example how to use this library: (Credits to: 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 ) );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With