25

Suppose I have a function which takes variadic arguments (...) or a va_list passed from another such function. The main logic is in this function itself (let's call it f1), but I want to have it pass the va_list to another function (let's call it f2) which will determine the next argument type, obtain it using va_arg, and properly convert and store it for the caller to use.

Is it sufficient to pass a va_list to f2, or is it necessary to pass a pointer to va_list. Unless va_list is required to be an array type or else store its position data at the location the va_list object points to (rather than in the object itself), I can't see how passing it by value could allow the calling function (f1) to 'see' the changes the called function made by va_arg.

Can anyone shed light on this? I'm interested in what the standard requires, not what some particular implementation allows.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711

7 Answers7

28

It looks like you'll need to pass a pointer to the va_list. For more info, see the C99 standard document section 7.15.In particular, bullet point 3 states:

The object ap may be passed as an argument to another function; if that function invokes the va_arg macro with parameter ap, the value of ap in the calling function is indeterminate and shall be passed to the va_end macro prior to any further reference to ap

[my italics]

Edit: Just noticed a footnote in the standard:

215) It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns

So you can pass a pointer to the va_list and do va_arg(*va_list_pointer) in the called function.

caf
  • 233,326
  • 40
  • 323
  • 462
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Agreed with your last edit - a pointer is the way to go (since the OP wants the changes to the `va_list` to be definitely visible in the calling function). – caf Jul 30 '10 at 08:48
  • @caf: I don't think you should have edited out my reference to `vfprintf()` because it looks like others are making the same mistake as I did and suggesting the questioner look at the existing prototypes (which doesn't actually answer his question). – JeremyP Jul 30 '10 at 09:18
  • 2
    I'd say leave a comment and/or vote 'em down then - it seems better to have the correct answer not obfuscated with the earlier, incorrect assumption... – caf Jul 30 '10 at 12:05
2

In my understanding, you're supposed to pass the va_list directly (not a pointer to it). This seems to be supported by comp.lang.c:

"A va_list is not itself a variable argument list; it's really sort of a pointer to one. That is, a function which accepts a va_list is not itself varargs, nor vice versa. "

S.C. Madsen
  • 5,100
  • 5
  • 32
  • 50
  • 2
    I don't think that quote supports your contention. Nothing about it disallows passing a pointer to a `va_list`, and indeed the standard explicitly says this is allowed. – caf Jul 30 '10 at 12:08
2

I find the texts quite ambiguous on this question. The simplest is perhaps to look in the standard how predefined functions with va_list are supposed to receive it, e.g vsnprintf. And this is clearly by value and not by reference.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 3
    The standard functions are not intended to be called in the way the OP wants - after they are called, the `va_list` can no longer be used and must only be passed to `va_end()`. – caf Jul 30 '10 at 12:07
  • 1
    Indeed, I'm the OP and I agree with caf on this one. JeremyP is the only one who seems to have cited any evidence and the standard is pretty clear that you have to pass a pointer for the semantics I want. – R.. GitHub STOP HELPING ICE Jul 30 '10 at 12:28
2

You should pass a pointer to a va_list if you want to use it in a subfunction and then not have to immediately pass it to va_end afterwards. From C99:

It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns.

The standard allows this, however, on some 64-bit platforms where va_list is an array type, this does not work. As the address of an array is the same as the address of the first element of the array, passing a pointer to the va_list will segfault upon calling va_arg with that pointer to va_list as an argument.

A way to get around this is by receiving the va_list as an unconventional argument name (usually suffixed with one or more underscores) and then creating a new local va_list, like so:

#include <stdarg.h>

int vfoo(va_list ap_)
{
    int ret;
    va_list ap;
    va_copy(ap, ap_);
    ret = vbar(&ap);
    /* do other stuff with ap */
    va_end(ap);
    return ret;
}

This is the approach I use in my vsnprintf implementation to call other functions from it for formatting.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • Thanks! We have hit the very same issue and your solution worked perfectly! However, I don't really understand what's going on. Even if `va_list` is implemented as an array, how is taking the address of a local variable (here: `&ap`) different from taking the address of a function argument (here: `&ap_`)? – Spacechild1 Sep 12 '22 at 09:00
  • 1
    Got it! An array argument automatically decays to a pointer, but the array on the stack does not, so the `&` operator yields different results. – Spacechild1 Sep 13 '22 at 15:07
0

Functions in standard C library pass va_list element itself (man 3 vprintf):

   #include <stdarg.h>

   int vprintf(const char *format, va_list ap);
   int vfprintf(FILE *stream, const char *format, va_list ap);
   int vsprintf(char *str, const char *format, va_list ap);
   int vsnprintf(char *str, size_t size, const char *format, va_list ap);
  • 8
    None of these functions are intended to be called in the way the OP wants - after these functions are called, the `va_list` can no longer be used and must only be passed to `va_end()`. – caf Jul 30 '10 at 12:06
0

Passing a pointer to va_list works fine in 32 bit system. You can even fetch one parameter a time in the sub-routine. But it doesn't seem to work in 64 bit system, will produce a segment fault at va_arg().

TerryYin
  • 174
  • 2
  • 6
  • The solution proposed in https://stackoverflow.com/a/57805088/6063908 seems to work fine for us on Linux, macOS and Windows. Do you know of any platform where this would not work? Honestly curious. – Spacechild1 Sep 12 '22 at 09:02
0

Neither, as va_list is either a pointer (eg x86) or an array (eg amd64); if you want to get rid of this ambiguity, wrap it into a structure, and pass the address of the structure.

typedef struct va_list_struct {
    va_list ap; /* platform_dependent: pointer _or_ array */
} va_list_struct;

void function2 (va_list_struct *myap);

void function1_v (va_list ap)
{
    va_list_struct myap;
    va_copy (myap.ap, ap);
    function2 (&myap);
    va_end (myap.ap);
}
Lorinczy Zsigmond
  • 1,749
  • 1
  • 14
  • 21