views:

76

answers:

2

It come across to me that function like printf() have not limited the number of parameters.

But when debugging program on Solaris, I noticed it will push at most 5 parameters into stack, common register will be used if there are more than 5 parameters.

So what will happen if even common register is not enough in function like printf ? Did compiler do something for me ?

+4  A: 

The behaviour is controlled by the ABI for the platform. If there are more parameters than fit in the registers, then they will be handled in a different way. There isn't a simple upper limit on the number of arguments that can be passed, so the compiler and the ABI define a mechanism that works on the hardware in question. What works on SPARC does not necessarily work on, for example, Intel IA32.

Jonathan Leffler
A: 

Normally platforms where the ABI uses registers for argument passing switch to a different calling convention for variadic functions, whereby everything is passed on the stack. This is why the C standard assigned undefined behavior to calling a variadic function without a prototype; without a prototype, on such platforms the compiler will generate an incorrect call.

It should be noted that some platforms use more complicated (uselessly complicated, I would say) methods of passing arguments to variadic functions, such as constructing a sort of linked list and passing a hidden pointer to that list, which the implementation of va_start is then somehow able to obtain. As a programmer, you should just treat the whole stdarg.h stuff as a black box that does what's expected, and pray that you never have to see the gorey details of some of the uglier implementations...

R..