As arguments are passed on the stack, the va_
"functions" (they are most of the time implemented as macros) simply manipulate a private stack pointer. This private stack pointer is stored from the argument passed to va_start
, and then va_arg
"pops" the arguments from the "stack" as it iterates the parameters.
Lets say you call the function max
with three parameters, like this:
max(a, b, c);
Inside the max
function, the stack basically looks like this:
+-----+
| c |
| b |
| a |
| ret |
SP -> +-----+
SP
is the real stack pointer, and it's not really a
, b
and c
that on the stack but their values. ret
is the return address, where to jump to when the function is done.
What va_start(ap, n)
does is take the address of the argument (n
in your function prototype) and from that calculates the position of the next argument, so we get a new private stack pointer:
+-----+
| c |
ap -> | b |
| a |
| ret |
SP -> +-----+
When you use va_arg(ap, int)
it returns what the private stack pointer points to, and then "pops" it by changing the private stack pointer to now point at the next argument. The stack now look like this:
+-----+
ap -> | c |
| b |
| a |
| ret |
SP -> +-----+
This description is of course simplified, but shows the principle.
va_arg
set of functions for variable arguments can differ between machine architectures and the call conventions, the ABI, that is used with particular processors. Modern processors with more registers available than the old x86 architecture allow for passing arguments in registers as well as on the stack. – Coursing