some inner thoughts on va_start()

Discussion in 'C++' started by Sekhar, Jun 30, 2006.

  1. Sekhar

    Sekhar Guest

    While using variable arguments we have to initialize variable argument
    like

    va_start( arg_ptr, prevParam );

    Can any body explain what is the significance of second
    parameter(prevParam) while initialization of variable arguments.


    Following is an extract from msdn, i am not able to understand the
    relevance of first parameter in the function Average() and how it is
    used while initialization of variable arguments.

    int main( void )
    {
    printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );
    }

    int average( int first, ... )
    {
    int count = 0, sum = 0, i = first;
    va_list marker;

    va_start( marker, first ); /* Initialize variable arguments. */
    while( i != -1 )
    {
    sum += i;
    count++;
    i = va_arg( marker, int);
    }
    va_end( marker ); /* Reset variable arguments. */
    return( sum ? (sum / count) : 0 );
    }
    Sekhar, Jun 30, 2006
    #1
    1. Advertising

  2. * Sekhar:
    > While using variable arguments we have to initialize variable argument
    > like
    >
    > va_start( arg_ptr, prevParam );
    >
    > Can any body explain what is the significance of second
    > parameter(prevParam) while initialization of variable arguments.


    For stack-based argument passing it provides a known address on the
    stack. From this address the address of the first ... argument can be
    easily found. It all depends on the implementation.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 30, 2006
    #2
    1. Advertising

  3. Sekhar

    Ron Natalie Guest

    Sekhar wrote:
    > While using variable arguments we have to initialize variable argument
    > like
    >
    > va_start( arg_ptr, prevParam );
    >


    The varargs functions were disgusting hacks added AFTER the language
    was implemented. On the original PDP-11 implementations you could
    just dig around on the stack to find the rest of the args so you didn't
    really need any compiler support, but you did need at least one real
    arg to take the address so you could start mining). Of course, when
    C started to get more standardized, they reallized that RISC chips
    and other argument passing would need real varargs compiler assistance,
    they added the compiler syntax (such as the ... declaration). The
    existing practice however wasn't much altered (although there are
    slight differences between the original varargs.h and the now
    standard stdarg.h macros).
    Ron Natalie, Jun 30, 2006
    #3
  4. Sekhar

    Joe Guest

    The second parameter accomplishes two things. 1) It provides a handle
    to the stack the arguments are on and 2) even if that were not
    required, it lets the va_args stuff know where the variable arguments
    start. That is if I had a function:

    void args(int a, char * p, ...)

    it's convenient to skip over a and p before processing the var args.

    joe
    Joe, Jun 30, 2006
    #4
  5. Sekhar

    Yong Hu Guest

    Sekhar wrote:
    > While using variable arguments we have to initialize variable argument
    > like
    >
    > va_start( arg_ptr, prevParam );
    >
    > Can any body explain what is the significance of second
    > parameter(prevParam) while initialization of variable arguments.
    >
    >
    > Following is an extract from msdn, i am not able to understand the
    > relevance of first parameter in the function Average() and how it is
    > used while initialization of variable arguments.
    >
    > int main( void )
    > {
    > printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );
    > }
    >
    > int average( int first, ... )
    > {
    > int count = 0, sum = 0, i = first;
    > va_list marker;
    >
    > va_start( marker, first ); /* Initialize variable arguments. */
    > while( i != -1 )
    > {
    > sum += i;
    > count++;
    > i = va_arg( marker, int);
    > }
    > va_end( marker ); /* Reset variable arguments. */
    > return( sum ? (sum / count) : 0 );
    > }



    Here is one simple implementation in VC:

    #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
    #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t))
    )
    #define va_end(ap) ( ap = (va_list)0 )
    Yong Hu, Jun 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frankg
    Replies:
    4
    Views:
    382
    Joe Durusau
    Jul 28, 2003
  2. John Harrison

    va_start and references

    John Harrison, Feb 3, 2004, in forum: C++
    Replies:
    6
    Views:
    1,779
    Ron Natalie
    Feb 4, 2004
  3. Ryan Liu

    va_start issue in GCC 3.3.1

    Ryan Liu, Feb 24, 2004, in forum: C++
    Replies:
    5
    Views:
    5,441
    Jack Klein
    Feb 25, 2004
  4. Douwe
    Replies:
    3
    Views:
    734
    Chris Torek
    Aug 30, 2003
  5. Minti

    Why va_start takes 2 arguments

    Minti, Sep 3, 2004, in forum: C Programming
    Replies:
    1
    Views:
    351
    Ben Pfaff
    Sep 3, 2004
Loading...

Share This Page