va_list usage

J

j0mbolar

given this example:

void bar(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
baz(fmt, ap);

va_end(ap);
}

void baz(const char *fmt, va_list ap)
{
char msg[MSG_SIZE];

vsnprintf(msg, sizeof msg, fmt, ap);

display(msg);
}

is this usage well defined? that is, passing a va_list
from one function and using it in another.


also, i've been wondering about something.
where in the standard is the following explained?

void func(int foo);

....

(****func)(foo); /* where is this method of calling func explained? */
 
M

Martin Dickopp

given this example:

void bar(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
baz(fmt, ap);

va_end(ap);
}

void baz(const char *fmt, va_list ap)
{
char msg[MSG_SIZE];

vsnprintf(msg, sizeof msg, fmt, ap);

display(msg);
}

is this usage well defined? that is, passing a va_list
from one function and using it in another.
Yes.

also, i've been wondering about something.
where in the standard is the following explained?

void func(int foo);

...

(****func)(foo); /* where is this method of calling func explained? */

6.3.2.1#4:
| A function designator is an expression that has function type. Except
| when it is the operand of the sizeof operator or the unary & operator,
| a function designator with type "function returning type" is converted
| to an expression that has type "pointer to function returning type".

6.5.3.2#4:
| The unary * operator denotes indirection. If the operand points to a
| function, the result is a function designator; [...].

6.5.2.2#1 (Function calls, Constraints):
| The expression that denotes the called function shall have type
| pointer to function returning void or returning an object type other
| than an array type.

6.5.2.2#3 (Function calls, Semantics):
| A postfix expression followed by parentheses () containing a possibly
| empty, comma-separated list of expressions is a function call. The
| postfix expression denotes the called function. The list of
| expressions specifies the arguments to the function.

The function designator `func', which has type `function returning
void', is not the operand of a `sizeof' or unary & operator, therefore
it is converted to type `pointer to function returning void'. The
innermost * operator is then applied; the result of the expression
`*func' has type `function returning void', but it is again not the
operand of a `sizeof' or unary & operator, so it is again converted to
type `pointer to function returning void'. This happens repeatedly.
Finally, the result of `(****func)' has type `pointer to function
returning void' (after conversion according to 6.3.2.1#4), and the
function is called according to 6.5.2.2#3.

Martin
 
D

Dave Thompson

given this example:

void bar(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);
baz(fmt, ap);

va_end(ap);
}

void baz(const char *fmt, va_list ap)
{
char msg[MSG_SIZE];

vsnprintf(msg, sizeof msg, fmt, ap);

display(msg);
}

is this usage well defined? that is, passing a va_list
from one function and using it in another.
Yes. Although to be really really pedantic, you should have a
prototype declaration of baz() in scope at (before) the call to it;
you aren't absolutely guaranteed va_list isn't a type altered by the
default argument promotions, though that's damn unlikely except on
DS9k. In C99 of course you must have some declaration, and it might as
well be prototype. And it's good >style< to always have prototype
declarations or definitions, but not required.

Also, for your particular example but not your general question,
vsnprintf is not standard before C99; though common in C90
implementations as an extension it is sometimes spelled differently or
has slightly different semantics.

If you go on to more complex cases than asked and shown, one thing
that is *not* well defined is using the same va_list in the caller
after the callee returns. In particular, it is not specified by the
Standard whether va_list is an array type and hence passed "by
reference" (that is, by decayed pointer) and shared, or a type like a
pointer or struct that is passed by value and not shared. If this is
an issue, pass an explicit pointer (and dereference) to force sharing,
or in C99 only use va_copy and a second va_list, in either the caller
or callee, to prevent it.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top