Variable arguments

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

    #1

    Variable arguments

    I am declaring a local buffer of a fixed size in my variable argument
    routine. How do I figure out the correct size of the variable argument list
    so that I can dynamically allocate the memory?

    void disp_message(ch ar *szCaption, char *szFormat, ...)
    {
    va_list ap;
    char szBuffer[4096] = { 0 };

    va_start(ap, szFormat);
    vsprintf(szBuff er, szFormat, ap);
    va_end(ap);

    // Display the message.
    MessageBox(NULL , szBuffer, szCaption, MB_OK | MB_ICONWARNING) ;
    }


  • Eric Sosman

    #2
    Re: Variable arguments

    frank wrote:[color=blue]
    > I am declaring a local buffer of a fixed size in my variable argument
    > routine. How do I figure out the correct size of the variable argument list
    > so that I can dynamically allocate the memory?[/color]

    Try vsnprintf(). It's a C99 addition, but is available
    in some pre-C99 implementations , too. Use some caution with
    pre-C99 versions, since their semantics may not be exactly
    those that were eventually codified in the C99 Standard.
    [color=blue]
    > void disp_message(ch ar *szCaption, char *szFormat, ...)
    > {
    > va_list ap;
    > char szBuffer[4096] = { 0 };[/color]

    The initialization is pointless (because you're about
    to overwrite the buffer anyhow) and potentially wasteful
    (because you're about to overwrite all those carefully-
    inserted zeroes).
    [color=blue]
    > va_start(ap, szFormat);
    > vsprintf(szBuff er, szFormat, ap);
    > va_end(ap);
    >
    > // Display the message.
    > MessageBox(NULL , szBuffer, szCaption, MB_OK | MB_ICONWARNING) ;
    > }[/color]

    --
    Eric.Sosman@sun .com

    Comment

    Working...