[Solved] Variable arguments in C functions


c itself doesn’t specify things like “heap” or “stack”, so programming standard and portable c, you should better think in categories of the c standard: static, automatic and dynamic storage.

Nevertheless, in a typical implementation, “automatic storage” translates to “the stack is used for it”. This is the case for function arguments and variadic functions are no exception here.

The reason va_end() might free some dynamic storage (typically: allocated on the heap) is that the va_arg() macro typically needs some context information to find the next argument. va_start() will allocate the memory for that information (not for the arguments themselves) and initialize it in a way so that the first va_arg() call returns the first variadic argument.

Note that an implementation of va_start() doesn’t have to allocate memory. va_list might be defined in a way that it provides the space for the required context information. But that should be of no interest at all to you as the programmer, all the va_* stuff is a black box for you and if the documentation states “call va_end() when done for cleanup”, you just do it 😉

solved Variable arguments in C functions