va_list passed by value/reference 
Author Message
 va_list passed by value/reference

Hello,

I'd like to do something like this:

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

  void func(const char *f1, const char *f2, ...)
  {
    va_list ap;

    va_start(ap, f2);
    vprintf(f1, ap);
    vprintf(f2, ap);
    va_end(ap);
  }

So:

  func("Line 1: %s\n", "Line 2: %s\n", "Hello", "Goodbye");

gives:

  Line 1: Hello
  Line 2: Goodbye

Is ap effectively passed by reference, so that the first vprintf() will
advance it by an appropriate number of arguments, ready for the second
call using the second format string?

If it's passed by value, is there any straight-forward way of achieving
the desired behaviour?

Thanks,

Steven
--



Sat, 13 Oct 2001 03:00:00 GMT  
 va_list passed by value/reference

Quote:

>Is [a va_list object] effectively passed by reference, so that the
>first vprintf() will advance it by an appropriate number of arguments,
>ready for the second call using the second format string?

Yes: PowerPC, Pyramid, V9 SPARC, no doubt others.  No: x86 PCs,
V[678] SPARC, most or all 680x0 systems, etc.

Neither: <fill in the blank>

In short, the Standard does not say what mechanism is used to implement
"va_list" objects, and says only that a va_list value is "dead" after
passing it to another function that uses it (such as vprintf).  All
you are allowed to do at this point is va_end(ap).

Quote:
>If it's passed by value, is there any straight-forward way of achieving
>the desired behaviour?

If you were calling your own function (not the v*printf family), you
could try passing the *address* of the "va_list" object; presumably
calls of the form:

        va_list *app;
        ...
        i = va_arg(*app, int);
        ...
        s = va_arg(*app, char *);
        ...

would leave "*app" modified in the desired manner.  Given how
magical "va_list"s are allowed to be, I would be a bit chary of
doing this myself.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc


--



Sun, 14 Oct 2001 03:00:00 GMT  
 va_list passed by value/reference


Quote:
>>If it's passed by value, is there any straight-forward way of achieving
>>the desired behaviour?
>If you were calling your own function (not the v*printf family), you
>could try passing the *address* of the "va_list" object; presumably
>calls of the form:
>    va_list *app;
>    ...
>    i = va_arg(*app, int);
>    ...
>    s = va_arg(*app, char *);
>    ...
>would leave "*app" modified in the desired manner.  Given how
>magical "va_list"s are allowed to be, I would be a bit chary of
>doing this myself.

Empirically, it doesn't work everywhere, however, polling of the committee
reveals that no one can imagine how an implementor could expect to get away
with not having it work.  I believe C9X says, at least in a footnote, that
"obviously" it will work.  Not normative, but an inevitable consequence of the
text describing the semantics of va_list things.

-s
--

C/Unix wizard, Pro-commerce radical, Spam fighter.  Boycott Spamazon!
Will work for interesting hardware.  http://www.plethora.net/~seebs/
Visit my new ISP <URL:http://www.plethora.net/> --- More Net, Less Spam!
--



Sun, 14 Oct 2001 03:00:00 GMT  
 va_list passed by value/reference

says...

Quote:
> Hello,

> I'd like to do something like this:

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

>   void func(const char *f1, const char *f2, ...)
>   {
>     va_list ap;

>     va_start(ap, f2);
>     vprintf(f1, ap);
>     vprintf(f2, ap);
>     va_end(ap);
>   }

[ ... ]

To make a long story short, you can't do this.  If you pass a va_list
to vprintf (or one of its cousins) you can't use the contents of the
va_list afterwards (at least not portably).  It might be advanced to
the next available argument, or it might be unchanged, or it might be
something else entirely.

OTOH, all printf company do at the end of a format string is quit
processing.  You can get the effect you want by concatenating the two
format strings into one, then passing the result along with the
arguments in a single call to vprintf.
--



Tue, 16 Oct 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Passing values by reference from C# to a C++ managed application

2. passing values by reference ???

3. Return by value versus passing a reference

4. Passing by value & reference

5. passing values by reference

6. Passing value as reference

7. passing return value to reference?

8. passing an array of structs - Is it by value or reference

9. Passing values by reference from C# to a C++ managed application

10. Portable to pass va_list (stdarg.h) variables?

11. Passing va_list to subroutine?

12. a va_list in a va_list

 

 
Powered by phpBB® Forum Software