Thread: Do I need to va_end before va_start ?

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

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed

Tags for this Thread