Thread: Forwarding variable parameter list.

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    Question Forwarding variable parameter list.

    Hi,

    I'm trying to achieve a function that does the following:

    Code:
    void myFunc(const char* str, ...)
    {
        printf(str, ...);
    }
    That is, I just want to pass the entire optional parameter list on unmolested. I've read lots about how to unpack the parameters, but I haven't been successful in doing something similar to the above.

    --- In more detail ---

    I'm using and API that provides a logging function, but uses unsafe string formatting routines, so a single % can cause the program to crash.

    So I want to do the following to introduce memory safety, and some #define convienience.

    Code:
    #ifdef DEBUG
    void myFunc(const char* msg, ...)
    {
         char largeArray[500];
        //Notice snprintf is memory safe
         snprintf(largeArray, 500, msg, ...);  //This, sadly, won't compile
         API_logging_function(largeArray);
    }
    #else
    void myFunc(const char* msg, ...) {return;} 
    #endif
    Any help appreciated - and yes, I'm sure there are typo's in the above but I hope my question is clear!

    Cheers,

    HappyClappers.

  2. #2

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    3

    Thumbs down

    Very nice, but doesn't answer my question. I know how to unpack the arguments. I don't want to unpack them. I wan't to pass the whole lot straight to another function.

    If I unpack them, then I have to do the work of parsing a format string to work out how many arguments are in the list, loop through them, and concatenate the bits of string together myself.

    I realize the answer is probably in stdarg.h, for example I have tried the va_list object (usually called ap) initialized by va_start(), a pointer to the list and some other combinatinos, but none work.

    That is

    Code:
    void myFunc(const char* str, ...)
    {
       va_list ap;
       va_start(ap, str); //Point to the first element after str
       printf(str, ap);      // Interprets ap for what it is: a memory address.
       va_end(ap);
    }
    
    // myFunc("%X", 0xAABBCCDDE)  has output 0x14372A6BB or whatever the actual address is
    // myFunc("%d %f", 2, 3.)   crashes because the printf function is only passed one extra parameter

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    That's the point. You have to unpack them youself. Your function doesn't know how many parameters it received. That's why you need the macros to read them all off according to some outside help, such as a format string like printf() and scanf() use, and then do whatever with them.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    3
    Thanks Dave_Sinkula, that answers my question: not possible.

    However, vsprintf looks like it could be an option, if only I can find a memory safe version.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Like vsnprintf() perhaps?
    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

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM