va_list fun
David O'Brien
obrien at FreeBSD.org
Tue Feb 15 22:05:12 PST 2005
On Wed, Feb 16, 2005 at 02:16:01AM +0100, Pav Lucistnik wrote:
> Someone please explain to me what I'm doing wrong:
>
> [legrace.c]
> #include <stdio.h>
> #include <stdarg.h>
>
> void funny(char *format, ...) {
> va_list ap;
>
> va_start(ap, format);
> vfprintf(stdout, format, ap);
> vfprintf(stdout, format, ap);
> vfprintf(stdout, format, ap);
> vfprintf(stdout, format, ap);
> vfprintf(stdout, format, ap);
> va_end(ap);
> }
You've consumed the 'ap' after the first vfprintf() can cannot resuse it.
You need to read the ISO C 1999 standard 7.15.
With all GCC supported platforms except PowerPC, and now AMD64 you can
get away with va_list copies via a simple '=' assignment. Now when you
want a copy you either have to use va_copy() [think about why you have to
use strcpy for two 'char *' vs. '=']. Or, va_end and do another
va_start, which would be cleaner in your case.
See /usr/src/sbin/dump/optr.c:msg() for an example of the correct way to
do it.
--
-- David (obrien at FreeBSD.org)
More information about the freebsd-amd64
mailing list