views:

186

answers:

5

Hi Everybody,

I attended a technical interview a few days ago, and I was asked How does the C compiler implments function with Variable number of arguments? How does it pass the on the stack?

Does anyone know or can explian that?

Thanks, Dan

+6  A: 

As far as I know, with C...

  • the caller function pushes arguments onto the stack in right-to-left order.

  • the caller is responsible for removing the arguments from the stack after the called function has executed. This is probably precisely because the caller is guaranteed to know how many arguments it put on the stack, while the called function might get it wrong.


P.S.: Calling conventions are usually implementation-specific. What I just described is known as the "cdecl" calling convention. Contrast this to a calling convention generally known as "stdcall", where the called function is responsible for removing its arguments from the stack. Because of that, it does not support variable-length argument lists.


P.P.S.: As user nategoose commented, I didn't mention how variable argument lists are actually used. See e.g. the POSIX documentation for the <stdarg.h> header for more information.

stakx
This is very clear, but you left out the callee's role. The callee (such as printf) has to use the one or more of the mandatory arguments (like the format string) to know what the parameters on the stack should be. It then uses target specific macros to access those as an array (the system stack is an array whose head you can move). The stdarg.h header contains the macros you need to do this, and often usually a pointer to the head of this array is passed to a function that expects to work on that (like vprintf).
nategoose
@nategoose: Thank you for your valuable addition. Just to let you know, I initially left this out on purpose, since the OP asked how the *compiler* implements variable argument lists, not how the *programmer* actually makes use of them. I've added a hyperlink to reference documentation now.
stakx
+5  A: 

It implements them using the va_ macros - for example va_start. Exactly what these macros do is implementation defined - in other words it will vary from CPU architecture to architecture, and from compiler to compiler. But they must play tricks with the C call stack. Typically, this will involve taking the address of the last named parameter as a base, and then accessing the variadic parameters by performing pointer arithmetic on this base.

anon
Yup, I remember that is why you always need one named parameter before the ...
dicroce
A: 

Look into va_start, va_arg, and va_end. Here is a ton of info on this.

Michael Dorgan
A: 

As far, as you sad that you got this question on tech interview, I'll assume that correct answer would be:

Caller will push explicit parameters to stack, count of variable parameters and variable parameters itself. Then target function code will be responsible for pop-ing all parameters out based on passed count and it's stack address.

And add a some ideas, why placing this parameters in separate array is not convenient.

Valera Kolupaev
+1  A: 

The Wikipedia article on stdarg.h might be helpful to you.

Ken