Do I need to va_end before va_start ?

This is a discussion on Do I need to va_end before va_start ? within the C Programming forums, part of the General Programming Boards category; Hi, everyone. I'm using a variable number of arguments to a function using <stdarg.h>. My code steps through the parameters ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Do I need to va_end before va_start ?

    Hi, everyone. I'm using a variable number of arguments to a function using <stdarg.h>.

    My code steps through the parameters twice. Do I need to call va_end () before I call va_start the second time?

    Richard

    Code:
    int myfunction ( int i , ... )
    {
        va_list va ;
        va_start ( va , i ) ;
    
        ...
    
        va_start ( va , i ) ;
    
        va_end ( va ) ;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Yes, you do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Better, don't ask such questions, just try those and then ask.....

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    It's better to read the standard to find out what is guaranteed to work, rather than merely trying stuff to see what your current compiler will let you get away with.

    Quote Originally Posted by draft c99
    7.15.1 Variable argument list access macros

    [#1] The va_start, va_arg, and va_copy macros described in
    this subclause shall be implemented as macros, not
    functions. It is unspecified whether va_end is a macro or
    an identifier declared with external linkage. If a macro
    definition is suppressed in order to access an actual
    function, or a program defines an external identifier with
    the name va_end, the behavior is undefined. Each invocation
    of the va_start or va_copy macros shall be matched by a
    corresponding invocation of the va_end macro
    in the function
    accepting a varying number of arguments.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21