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
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