An interesting question came up that I've done a bit of research on but cannot find a conclusive answer. I also have a suspicion as to the answer but I thought the SO crowd might actually know.
Is there actually a limit to the number of arguments passed to a variadic function?
All of the documentation that I can find is completely silent on this point. There are minimum numbers for supported arguments in the various C standards, but no maximum listed. I have already tested code with north of 60,000 arguments with no trouble at all.
My Hypothesis
I suspect that this is actually architecture dependent and would effectively be limited by the maximum stack size.
Edit
I may have sent people haring off in the wrong direction. My focus is not on the minimum but on whether or not they are aware of any actual maximum, whether that is based on a standard, compiler implementation or architecture.
No such limit is imposed by the C standard.
The C standard does require any conforming implementation to support some program that passes at least 127 arguments in a function call (see N1570 section 5.2.4.1), but that's neither an upper nor a lower bound for variadic functions; the call needn't be to a variadic function, and implementations are free to support more than 127 arguments. The section that defines <stdarg.h>, 7.16, doesn't mention any limits.
In practice, any limit is likely to be imposed by available resources, either at compile time (when compiling the call) or at run time (when executing it).
And simply due to common sense and market forces, compilers are likely to support arbitrarily long argument lists rather than imposing some fixed limit. Once you're required to support 127 arguments, it's probably easier to allocate any needed data structures dynamically than to use fixed-size structures. (The point of the translation limits in 5.2.4.1 is, in my opinion, to encourage compilers not to impose fixed limits at all.)
On the other hand, a conforming compiler could impose an unreasonable small limit, and a compiler for a very small embedded system might even have a valid reason to do so.
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