Here I found an example of how varargs can be used in C.
#include <stdarg.h>
double average(int count, ...)
{
va_list ap;
int j;
double tot = 0;
va_start(ap, count); //Requires the last fixed parameter (to get the address)
for(j=0; j<count; j++)
tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument.
va_end(ap);
return tot/count;
}
I can understand this example only to some extent.
It is not clear to me why we use
va_start(ap, count);
. As far as I understand, in this way we set the iterator to its first element. But why it is not set to the beginning by default?It is not clear to me why we need to give
count
as an argument. Can't C automatically determine the number of the arguments?It is not clear to me why we use
va_end(ap)
. What does it change? Does it set the iterator to the end of the list? But is it not set to the end of the list by the loop? Moreover, why do we need it? We do not useap
anymore; why do we want to change it?
Remember that arguments are passed on the stack. The
va_start
function contains the "magic" code to initialize theva_list
with the correct stack pointer. It must be passed the last named argument in the function declaration or it will not work.What
va_arg
does is use this saved stack pointer, and extract the correct amount of bytes for the type provided, and then modifyap
so it points to the next argument on the stack.In reality these functions (
va_start
,va_arg
andva_end
) are not actually functions, but implemented as preprocessor macros. The actual implementation also depends on the compiler, as different compilers can have different layout of the stack and how it pushes arguments on the stack.Maybe because of historical reasons from when compilers weren't smart enough. Maybe because you might have a varargs function prototype which doesn't actually care about the varargs and setting up varargs happens to be expensive on that particular system. Maybe because of more complex operations where you do
va_copy
or maybe you want to restart working with the arguments multiple times and callva_start
multiple times.The short version is: because the language standard says so.
That's not what all that
count
is. It is the last named argument to the function.va_start
needs it to figure out where the varargs are. Most likely this is for historical reasons on old compilers. I can't see why it couldn't be implemented differently today.As the second part of your question: no, the compiler doesn't know how many arguments were sent to the function. It might not even be in the same compilation unit or even the same program and the compiler doesn't know how the function will be called. Imagine a library with a varargs function like
printf
. When you compile your libc the compiler doesn't know when and how programs will callprintf
. On most ABIs (ABI is the conventions for how functions are called, how arguments are passed, etc) there is no way to find out how many arguments a function call got. It's wasteful to include that information in a function call and it's almost never needed. So you need to have a way to tell the varargs function how many arguments it got. Accessingva_arg
beyond the number of arguments that were actually passed is undefined behavior.On most architectures
va_end
doesn't do anything relevant. But there are some architectures with complex argument passing semantics andva_start
could even potentially malloc memory then you'd needva_end
to free that memory.The short version here is also: because the language standard says so.
va_start initalises the list of variable arguments. You always pass the last named function argument as the second parameter. It is because you need to provide information about location in the stack, where variable arguments begin, since arguments are pushed on the stack and compiler cannot know where there's a beginning of variable argument list (there's no differentiation).
As to va_end, it is used to free resources allocated for the variable argument list during the va_start call.
It's C macroses.
va_start
sets internal pointer to address of first element.va_end
cleanupva_list
. If there isva_start
in code and there is nova_end
- it's UB.