help with va_list etc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    #1

    help with va_list etc

    Can anyone point me to any good online references
    about the va_arg() va_list() functions/macros

    I googled and found a couple of things, but what I found, failed to explain
    how it really works and give a easy to follow example program

    Any pointers appreciated.




  • The Dragon

    #2
    Re: help with va_list etc

    Alex wrote:[color=blue]
    > Can anyone point me to any good online references
    > about the va_arg() va_list() functions/macros
    >
    > I googled and found a couple of things, but what I found, failed to explain
    > how it really works and give a easy to follow example program
    >
    > Any pointers appreciated.
    >
    >
    >
    >[/color]
    #include <stdarg.h>

    /* yadda yadda, main, and prototypes, and all that jazz */


    int foo_varargs( char *fmt, ... ) {
    va_list ap, bp;
    int foo;

    /* This starts the "va_*" functions up, the format
    is your va_list, and the last argument defined before ... */
    va_start( ap, fmt );


    /* Now, you can get an arg from the list by type etc... It will
    forward to the next argument, and va_arg will automagically cast
    to "type", (in this case int), hence why you can pass
    an integer where you should have passed a string where you
    defined %s in the format string, and endup with a GPF, or
    segv ;} */
    foo = va_arg( &ap, int );


    /* Or copy one list to another... */
    va_copy( bp, ap );


    /* And when you're done, do this */
    va_end( ap );


    /* Got it? */
    return 0;
    }


    Most of the junk that is in a format string for such functions
    is parsed inside the function too, so you get the idea.

    -- The Dragon

    Comment

    • Alex

      #3
      Re: help with va_list etc

      Thanks for the reply!

      How do you determine when you've reached the end of the arguement list?

      say you parse 5 arguements to a function, but the code in the function is
      processing
      each arguement till it reaches the end, how could code be written to check
      if the arguement
      being read, is the last one?


      "The Dragon" <noemail@nospam .com> wrote in message
      news:ncmdnRmgJs UJN07cRVn-tQ@rogers.com.. .[color=blue]
      > Alex wrote:[color=green]
      > > Can anyone point me to any good online references
      > > about the va_arg() va_list() functions/macros
      > >
      > > I googled and found a couple of things, but what I found, failed to[/color][/color]
      explain[color=blue][color=green]
      > > how it really works and give a easy to follow example program
      > >
      > > Any pointers appreciated.
      > >
      > >
      > >
      > >[/color]
      > #include <stdarg.h>
      >
      > /* yadda yadda, main, and prototypes, and all that jazz */
      >
      >
      > int foo_varargs( char *fmt, ... ) {
      > va_list ap, bp;
      > int foo;
      >
      > /* This starts the "va_*" functions up, the format
      > is your va_list, and the last argument defined before ... */
      > va_start( ap, fmt );
      >
      >
      > /* Now, you can get an arg from the list by type etc... It will
      > forward to the next argument, and va_arg will automagically cast
      > to "type", (in this case int), hence why you can pass
      > an integer where you should have passed a string where you
      > defined %s in the format string, and endup with a GPF, or
      > segv ;} */
      > foo = va_arg( &ap, int );
      >
      >
      > /* Or copy one list to another... */
      > va_copy( bp, ap );
      >
      >
      > /* And when you're done, do this */
      > va_end( ap );
      >
      >
      > /* Got it? */
      > return 0;
      > }
      >
      >
      > Most of the junk that is in a format string for such functions
      > is parsed inside the function too, so you get the idea.
      >
      > -- The Dragon[/color]


      Comment

      • Mike H

        #4
        Re: help with va_list etc


        "Alex" <yep@yep.com> wrote in message
        news:41d3b1d3$0 $11881$afc38c87 @news.optusnet. com.au...[color=blue]
        > Thanks for the reply!
        >
        > How do you determine when you've reached the end of the arguement list?
        >
        > say you parse 5 arguements to a function, but the code in the function is
        > processing
        > each arguement till it reaches the end, how could code be written to check
        > if the arguement
        > being read, is the last one?[/color]

        Just test for the null string terminator in fmt. If all the data types will
        be the same then you could just pass the number of arguments instead of a
        format string.

        Mike H.

        [color=blue]
        > "The Dragon" <noemail@nospam .com> wrote in message
        > news:ncmdnRmgJs UJN07cRVn-tQ@rogers.com.. .[color=green]
        >> Alex wrote:[color=darkred]
        >> > Can anyone point me to any good online references
        >> > about the va_arg() va_list() functions/macros
        >> >
        >> > I googled and found a couple of things, but what I found, failed to[/color][/color]
        > explain[color=green][color=darkred]
        >> > how it really works and give a easy to follow example program
        >> >
        >> > Any pointers appreciated.
        >> >
        >> >
        >> >
        >> >[/color]
        >> #include <stdarg.h>
        >>
        >> /* yadda yadda, main, and prototypes, and all that jazz */
        >>
        >>
        >> int foo_varargs( char *fmt, ... ) {
        >> va_list ap, bp;
        >> int foo;
        >>
        >> /* This starts the "va_*" functions up, the format
        >> is your va_list, and the last argument defined before ... */
        >> va_start( ap, fmt );
        >>
        >>
        >> /* Now, you can get an arg from the list by type etc... It will
        >> forward to the next argument, and va_arg will automagically cast
        >> to "type", (in this case int), hence why you can pass
        >> an integer where you should have passed a string where you
        >> defined %s in the format string, and endup with a GPF, or
        >> segv ;} */
        >> foo = va_arg( &ap, int );
        >>
        >>
        >> /* Or copy one list to another... */
        >> va_copy( bp, ap );
        >>
        >>
        >> /* And when you're done, do this */
        >> va_end( ap );
        >>
        >>
        >> /* Got it? */
        >> return 0;
        >> }
        >>
        >>
        >> Most of the junk that is in a format string for such functions
        >> is parsed inside the function too, so you get the idea.
        >>
        >> -- The Dragon[/color]
        >
        >[/color]


        Comment

        • Michael Mair

          #5
          Re: help with va_list etc



          Alex wrote:[color=blue]
          > Thanks for the reply!
          >
          > How do you determine when you've reached the end of the arguement list?
          >
          > say you parse 5 arguements to a function, but the code in the function is
          > processing
          > each arguement till it reaches the end, how could code be written to check
          > if the arguement
          > being read, is the last one?[/color]

          Please do not top-post. Write your answer below the text you are
          referring to.


          There are essentially 3 ways to communicate the number of arguments

          - explicit: one of the mandatory arguments is the number of
          (optional or total) arguments. Example:
          double sumN (int N, ... /* doubles */)
          {
          /* N>=0, sums up the optional arguments which are all
          ** of type double*/
          }
          - semi-explicit: one of the mandatory arguments contains
          information which gives you the number of optional arguments.
          Example:
          int printf(const char*restrict format, ...)
          {
          /* Note: restrict is a C99 keyword */
          }
          - implicit: the last argument has a certain form which tells
          you that it is the last. Example:
          /* UNIX system call for a program, <unistd.h> */
          int execl(const char *path, const char *arg, ... /* strings */);
          /* Explanation from the c.l.c FAQ 5.2:
          For example, the Unix system call
          execl takes a variable-length, null-pointer-terminated list of
          character pointer arguments, and is correctly called like this:
          execl("/bin/sh", "sh", "-c", "date", (char *)0);
          That is, the last argument is a null pointer of type (char *).
          */

          Note that in the first and last case, we restricted ourselves to
          a certain type for the variable arguments. The format string of
          printf also gives us the flexibility for arbitrary argument types.
          You can extend and/or combine the three ideas, but in the end, it
          comes down to those.


          Cheers
          Michael[color=blue]
          >
          >
          > "The Dragon" <noemail@nospam .com> wrote in message
          > news:ncmdnRmgJs UJN07cRVn-tQ@rogers.com.. .
          >[color=green]
          >>Alex wrote:
          >>[color=darkred]
          >>>Can anyone point me to any good online references
          >>>about the va_arg() va_list() functions/macros
          >>>
          >>>I googled and found a couple of things, but what I found, failed to[/color][/color]
          >
          > explain
          >[color=green][color=darkred]
          >>>how it really works and give a easy to follow example program
          >>>
          >>>Any pointers appreciated.
          >>>
          >>>
          >>>
          >>>[/color]
          >>
          >>#include <stdarg.h>
          >>
          >>/* yadda yadda, main, and prototypes, and all that jazz */
          >>
          >>
          >>int foo_varargs( char *fmt, ... ) {
          >>va_list ap, bp;
          >>int foo;
          >>
          >>/* This starts the "va_*" functions up, the format
          >> is your va_list, and the last argument defined before ... */
          >>va_start( ap, fmt );
          >>
          >>
          >>/* Now, you can get an arg from the list by type etc... It will
          >>forward to the next argument, and va_arg will automagically cast
          >>to "type", (in this case int), hence why you can pass
          >> an integer where you should have passed a string where you
          >>defined %s in the format string, and endup with a GPF, or
          >> segv ;} */
          >>foo = va_arg( &ap, int );
          >>
          >>
          >>/* Or copy one list to another... */
          >>va_copy( bp, ap );
          >>
          >>
          >>/* And when you're done, do this */
          >>va_end( ap );
          >>
          >>
          >>/* Got it? */
          >>return 0;
          >>}
          >>
          >>
          >>Most of the junk that is in a format string for such functions
          >>is parsed inside the function too, so you get the idea.
          >>
          >>-- The Dragon[/color]
          >
          >
          >[/color]


          --
          E-Mail: Mine is a gmx dot de address.

          Comment

          • Jonathan Burd

            #6
            Re: help with va_list etc

            Alex wrote:[color=blue]
            > Can anyone point me to any good online references
            > about the va_arg() va_list() functions/macros
            >
            > I googled and found a couple of things, but what I found, failed to explain
            > how it really works and give a easy to follow example program
            >
            > Any pointers appreciated.
            >
            >
            >
            >[/color]

            A good reference is www.delorie.com More specifically,
            Since 1983, developing the free Unix style operating system GNU, so that computer users can have the freedom to share and improve the software they use.


            Regards,
            Jonathan

            Comment

            • The Dragon

              #7
              Re: help with va_list etc

              Alex wrote:[color=blue]
              > Thanks for the reply!
              >
              > How do you determine when you've reached the end of the arguement list?
              >
              > say you parse 5 arguements to a function, but the code in the function is
              > processing
              > each arguement till it reaches the end, how could code be written to check
              > if the arguement
              > being read, is the last one?
              >[/color]

              Well, I dont see why you'd bother, since generally these functions have
              a format that comes through with it, but if yours doesn't, then it
              appears you might be up a stump.

              From a UNIX manpage:

              The va_arg macro expands to an expression that has the type and

              value of the next argument in the call. The parameter ap is
              the va_list ap initialized by va_start. Each call to va_arg
              modifies ap so that the next call returns the next argument.

              The parameter type is a type name specified so that the type of a
              pointer to an object that has the spec-ified type can be obtained
              simply by adding a * to type.

              The first use of the va_arg macro after that of the va_start
              macro returns the argument after last. Successive invocations
              return the values of the remaining arguments.

              If there is no next argument, or if type is not compatible
              with the type of the actual next argument (as promoted according
              to the default argument promotions), random errors will occur.

              If ap is passed to a function that uses va_arg(ap,type) then the
              value of ap is undefined after the return of that function.


              Ew... "Random Errors"... that doesn't sound pleasant. My suggestion?
              Either pass an arg count before the varargs, or pass a format string.

              Then you can simply ignore extra arguments =}

              - The Dragon

              Comment

              • Scott J. McCaughrin

                #8
                Re: help with va_list etc

                The Dragon <noemail@nospam .com> wrote:
                : Alex wrote:
                : > Can anyone point me to any good online references
                : > about the va_arg() va_list() functions/macros
                : >
                : >
                : #include <stdarg.h>

                . . . . .

                : int foo_varargs( char *fmt, ... ) {
                : va_list ap, bp;
                : int foo;

                : /* This starts the "va_*" functions up, the format
                : is your va_list, and the last argument defined before ... */
                : va_start( ap, fmt );

                Well, what this does is to set ap -> fmt. Note that the argument-list must
                be non-empty (containing at least one fixed argument). Usually, va_start()
                is used to set ap -> the final fixed argument.

                : /* Now, you can get an arg from the list by type etc... It will
                : forward to the next argument, and va_arg will automagically cast
                : to "type", (in this case int), hence why you can pass
                : an integer where you should have passed a string where you
                : defined %s in the format string, and endup with a GPF, or
                : segv ;} */
                : foo = va_arg( &ap, int );
                |
                You don't want the & ---' (should be: foo = va_arg(ap, int);
                This updates ap to point at the int-type argument (past `fmt').


                : /* Or copy one list to another... */
                : va_copy( bp, ap );


                : /* And when you're done, do this */
                : va_end( ap );

                Comment

                Working...