va_copy in called function

I

Ian Pilcher

Can va_copy be used to copy a va_list object that was initialized in a
different function? (I.e. the function in which va_copy is used is
called by the function in which the original va_list is initialized with
va_start.)

For example, assume I have an error logging function like this:

#include <stdarg.h>
#include <stdio.h>

extern int log_to_stderr;
extern FILE *log_file;

void log_error(const char *format, ...)
{
va_list ap;

if (log_to_stderr)
{
va_start(ap, format);
(void)vfprintf(stderr, format, ap);
va_end(ap);
}

if (log_file != NULL)
{
va_start(ap, format);
(void)vfprintf(log_file, format, ap);
va_end(ap);
}
}

Now I find that I need to create a wrapper to handle really critical
errors, so I do this:

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>

extern int log_to_stderr;
extern FILE *log_file;

static void vlog_error(const char *format, va_list ap)
{
va_list aq;

if (log_to_stderr)
{
va_copy(aq, ap);
(void)vfprintf(stderr, format, aq);
va_end(aq);
}

if (log_file != NULL)
(void)vfprintf(log_file, format, ap);
}

void log_error(const char *format, ...)
{
va_list ap;

va_start(ap, format);
vlog_error(format, ap);
va_end(ap);
}

void log_critical_error(const char *format, ...)
{
va_list ap;

va_start(ap, format);
vlog_error(format, ap);
va_end(ap);

log_error("CRITICAL ERROR - DYING!!!!!!!!!\n");

abort();
}

Note how va_start is used to initialize ap in log_error or
log_critical_error, but va_copy is used to copy ap to aq in vlog_error.
I can't find anything that says this isn't allowed, but I can't find
anything that specifically says it *is* allowed either, which always
makes me nervous.

TIA
 
C

Chris Torek

Can va_copy be used to copy a va_list object that was initialized in a
different function? ...

I believe so, yes. But if you want a Standardese Interpreter
Interpretation, rather than a C User's Opinion, the proper group
is comp.std.c. :)
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top