Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are variable arguments implemented in gcc?

int max(int n, ...) 

I am using cdecl calling convention where the caller cleans up the variable after the callee returns.

I am interested in knowing how do the macros va_end, va_start and va_arg work?

Does the caller pass in the address of the array of arguments as the second argument to max?

like image 939
Bruce Avatar asked Sep 11 '12 13:09

Bruce


People also ask

How va_ arg works?

The va_arg() macro retrieves a value of the given var_type from the location given by arg_ptr and increases arg_ptr to point to the next argument in the list. The va_arg() macro can retrieve arguments from the list any number of times within the function.

How does Va_arg work in C?

In the C Programming Language, the va_arg function fetches an argument in a variable argument list. The va_arg function updates ap so that the next call to the va_arg function fetches the next argument. You must call the va_start function to initialize ap before using the va_arg function.

How does Stdarg H work?

h is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type. C++ provides this functionality in the header cstdarg .

What is variable arguments in C?

Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement.


2 Answers

If you look at the way the C language stores the parameters on the stack, the way the macros work should become clear:-

Higher memory address    Last parameter                          Penultimate parameter                          ....                          Second parameter Lower memory address     First parameter        StackPointer  ->  Return address 

(note, depending on the hardware the stack pointer maybe one line down and the higher and lower may be swapped)

The arguments are always stored like this1, even without the ... parameter type.

The va_start macro just sets up a pointer to the first function parameter, e.g.:-

 void func (int a, ...)  {     // va_start    char *p = (char *) &a + sizeof a;  } 

which makes p point to the second parameter. The va_arg macro does this:-

 void func (int a, ...)  {     // va_start    char *p = (char *) &a + sizeof a;     // va_arg    int i1 = *((int *)p);    p += sizeof (int);     // va_arg    int i2 = *((int *)p);    p += sizeof (int);     // va_arg    long i2 = *((long *)p);    p += sizeof (long);  } 

The va_end macro just sets the p value to NULL.

NOTES:

  1. Optimising compilers and some RISC CPUs store parameters in registers rather than use the stack. The presence of the ... parameter would switch off this ability and for the compiler to use the stack.
like image 120
Skizz Avatar answered Sep 18 '22 12:09

Skizz


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.

like image 39
Some programmer dude Avatar answered Sep 22 '22 12:09

Some programmer dude