14

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!

Community
  • 1
  • 1
mozzbozz
  • 3,052
  • 5
  • 31
  • 44
  • as I know (and your first link says) the limit is 256 arguments in c++ and 127 in c. The line length is no problem since you can break a line into more lines. The stack size will only be a problem if you have huge structs as arguments (not pointer to the structs). – mch Sep 30 '14 at 13:37
  • That's what the standard states as "minimum" numbers (I linked the corresponding posts in my question). What I'd like to know is the "real" number, the limitations by a specific compiler. Line length: I'm not sure but I thought, the preprocessor does remove any linebreaks etc. anyway (the reason why we need the ";" at the end of every line) - so breaking them into multiple lines (which I've done of course) doesn't make a difference, I think. Stack size: Ok, I only need to pass pointers, so that doesn't seem to be a limitation. – mozzbozz Sep 30 '14 at 14:02
  • 3
    It is probably ABI, processor, and compiler specific. I guess that you have an x86-64. Then *in practice* the limit is related to stack frame and stack size, so I guess you might pass a few thousand arguments. And I suggest you to make a simple test: write a script generating, for some argument N, a function of N arguments in one file (e.g. computing the sum of all its arguments), and in another file a `main` calling that function. – Basile Starynkevitch Sep 30 '14 at 14:06
  • Why do you want you code to handle more than the language specified minimum? Certainly your code will need to enforce _some_ limit so it can flag unruly input. If it uses the spec minimum (127), what is the loss? – chux - Reinstate Monica Sep 30 '14 at 14:43
  • @chux: At the moment there are about 500 parameters and it works fine (though I had another bug I couldn't find for a couple of hours and therefore started thinking about other causes - e.g. the max number of parameters). They are unlikely to get a lot more but when I started thinking about this limitation, this topic arouse my interest and I asked this question. It's a wrapper function generated by Matlab and has to do something with S-Functions. But the generation of it is not my task (and I will make the others aware of this limit in the c standard, just in case)... – mozzbozz Sep 30 '14 at 14:56
  • Hmmmm, What should you code do if the parameter count is too high? Or does your code need to accept any number of parameters? – chux - Reinstate Monica Sep 30 '14 at 14:58
  • @chux: The number of parameters is given and is not variable. As long as it doesn't increase any more, there should be no problem (it's working right now). Otherwise the people giving this wrapper to me will need to see what they can change to split this function into multiple parts (which would not be very clean style either because there is no real alternative in this case)... – mozzbozz Sep 30 '14 at 15:11

3 Answers3

17

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).

mch
  • 9,424
  • 2
  • 28
  • 42
2

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 ) );
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • It seems that this would still run into the limit of 127 arguments passed to a single function call mentioned in @mch's answer though. – Mark Apr 26 '19 at 22:26
-3

standard did not know, but over the stack, you can pass any number of parameters, if pass through registers, the amount limited by the number of registers. In c language you can pass structures by value over stack.

Ivan Ivanovich
  • 880
  • 1
  • 6
  • 15
  • 8
    c standard 5.2.4.1 says: `127 parameters in one function definition` `127 arguments in one function call` – mch Sep 30 '14 at 13:55