This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: va_list question


"Bob Boz" <gcc_compiler@yahoo.ca> writes:

> First off, I hope this isn't off topic here.
> Is the code below supposed to work in general?

No.

>  I mean regardless of the
> compiler/platform (although the compiler used is gcc):
> 
> void f1 (const char *format, ...)
> {
>   int i=0;
>   va_list args;
>   va_start (args, format);
>   f2 (format, (va_list *)args);

You should have written "&args", not "(va_list *)args".

>   i = va_arg (args, int);
>   va_end (args);
> }
> 
> void f2 (const char *format, va_list * arg)
> {
> 
>    int j = va_arg (*arg, int);
> 
> }
> 
> My main point of worry is type casting 'arg' to 'va_list *' and then calling
> *arg inside f2.
> Specially that in gcc for ppc (now I can talk about compiler/platform)
> va_list is an array.
> 
> Using gcc.2.95.3 the above code crashes. However, when I change it to :
> 
> void f1 (const char *format, ...)
> {
>   int i=0;
>   va_list args;
>   va_start (args, format);
>   f2 (format, args);	<======
>   i = va_arg (args, int);
>   va_end (args);
> }
> 
> void f2 (const char *format, va_list arg)
> {
>    va_list ap;
>    __va_copy (ap, arg);
> 
>    int j = va_arg (ap, int);
> 
> }
> 
> Things seem to work ok.

Yes, this would work too.

> Although I beleive va_copying shouldn't be
> necessary. This should work as well.
> 
> void f2 (const char *format, va_list arg)
> {
>    int j = va_arg (arg, int);
> }

No, that won't always work.

-- 
- Geoffrey Keating <geoffk@geoffk.org> <geoffk@redhat.com>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]