Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Feb 2005 22:05:06 -0800
From:      "David O'Brien" <obrien@FreeBSD.org>
To:        Pav Lucistnik <pav@FreeBSD.org>
Cc:        freebsd-amd64@FreeBSD.org
Subject:   Re: va_list fun
Message-ID:  <20050216060506.GA2900@dragon.nuxi.com>
In-Reply-To: <1108516561.1564.4.camel@hood.oook.cz>
References:  <1108516561.1564.4.camel@hood.oook.cz>

next in thread | previous in thread | raw e-mail | index | archive | help
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@FreeBSD.org)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050216060506.GA2900>