I'd like to write a function that has return type of va_list.
example: va_list MyFunc(va_list args);
is this safe and portable?
I'd like to write a function that has return type of va_list.
example: va_list MyFunc(va_list args);
is this safe and portable?
va_list
might (but is not guaranteed to) be an array type, so you can't pass or return it by value. Code that looks as if it does, might just be passing/returning a pointer to the first element, so you can use the parameter in the callee but you might be acting on the original.
Formally you can probably say that va_list
is an entity type, not a value type. You copy it with va_copy
, not with assignment or via function parameters/return.
While you can definitely return
such a value, I am not sure if the return value can be used in a useful way.
As the handling of va_list
s requires special treatment (va_end()
required after va_start()
and va_copy()
), and va_start/copy
and va_end
macros are even allowed to contain { }
to enforce this pairing, you cannot call one without the other.
Whatever the language standard says, this is unlikely to work in practice. A va_list
is likely to be a pointer to a call record placed on the stack by a caller for the benefit of the callee. Once the callee returns, that memory on the stack is fair game for reuse.
Returning the type va_list
is unlikely to actually copy the content of the list back to the caller. Although that would be a valid implementation of C, if the standard requires it be done so, that would be a defect in the specification.
Passing a pointer to another function is quite different from returning that pointer though. Many/most implementations store the actual variable arguments in a stack frame that is destroyed when the vararg function returns. (i.e. returning a va_list or a pointer to one, will leave you with pointers to local variables that's destroyed). – nos
well in my case i'd be returning va_list back but thanks for warning – Hayri Uğur Koltuk
If you pass a pointer to a va_list
to the function MyFunc(va_list *args)
, you don't need to pass the modified (by va_arg(*args, type)
) argument list back, since MyFunc
modifies the original list.