creating va_list dynamically in GCC - can it be done?

Viewed 5399

my problem with vsprintf is that I can not obtain input arguments directly, I have to first get inputs one by one and save them in void**, then pass this void** to vsprintf(), it is all fine for windows, but when I come to 64bit linux, gcc cannot compile because it is not allowed to convert from void** to va_list, Is there anyone that can give me some help how I should do this under linux?

Can I create va_list dynamically in GCC?

void getInputArgs(char* str, char* format, ...)
{
    va_list args;
    va_start(args, format);
    vsprintf(str, format, args);
    va_end(args);
}  

void process(void)
{
    char s[256];
    double tempValue;
    char * tempString = NULL;
    void ** args_ptr = NULL;
    ArgFormatType format;   //defined in the lib I used in the code
    int numOfArgs = GetNumInputArgs();  // library func used in my code

    if(numOfArgs>1)
    {
        args_ptr = (void**) malloc(sizeof(char)*(numOfArgs-1));
        for(i=2; i<numOfArgs; i++)
        {
            format = GetArgType();    //library funcs

            switch(format)
            {
                case ArgType_double:
                    CopyInDoubleArg(i, TRUE, &tempValue);   //lib func
                    args_ptr[i-2] = (void*) (int)tempValue;    
                    break;

                case ArgType_char:
                    args_ptr[i-2]=NULL;
                    AllocInCharArg(i, TRUE, &tempString);  //lib func
                    args_ptr[i-2]= tempString;
                break;
            }
        }
    }

    getInputArgs(s, formatString, (va_list) args_ptr);   //Here 
           // is the location where gcc cannot compile, 
           // Can I and how if I can create a va_list myself?
}
5 Answers

Following class works for me:

class VaList
{
    va_list     _arguments;

public:

    explicit inline VaList(const void * pDummy, ...)
    {
        va_start(_arguments, pDummy);
    }

    inline operator va_list &()
    {
        return _arguments;
    }

    inline operator const va_list &() const
    {
        return _arguments;
    }

    inline ~VaList()
    {
        va_end(_arguments);
    }
};

and it can be used like this:

void v(const char * format, const va_list & arguments)
{
    vprintf(format, const_cast<va_list &>(arguments));
}

...

    v("%d\n", VaList("", 1)); // Uses VaList::operator va_list &()
    v("%d %d\n", VaList(nullptr, 2, 3)); // Uses VaList::operator va_list &()
    vprintf("%s %s %s\n", VaList("", "Works", "here", "too!"));

    const VaList args(NULL, 4, 5, "howdy", "there");
    v("%d %d %s %s\n", args); // Uses VaList::operator const va_list &() const

The first dummy parameter can be any kind of pointer, it is only used to compute the address of the following arguments.

The same can of course be done in C too but not so niftily (use pointer instead of reference)!

Simple example of using VaList to construct a dynamic va_list:

static void VectorToVaList(const std::vector<int> & v, va_list & t)
{
    switch (v.size())
    {
        case 1: va_copy(t, VaList("", v[0])); return;
        case 2: va_copy(t, VaList("", v[0], v[1])); return;
        case 3: va_copy(t, VaList("", v[0], v[1], v[2])); return;
        case 4: va_copy(t, VaList("", v[0], v[1], v[2], v[3])); return;
        // etc
    }

    throw std::out_of_range("Out of range vector size!");
}

and usage:

    va_list t;
    VectorToVaList(std::vector<int>{ 1, 2, 3, 4 }, t);
    vprintf("%d %d %d %d\n", t);

If the problem you're trying to solve is inserting passing arbitrary types to a function in va_list style, then, consider using union:

#include <iostream>
#include <cstdarg>

union ARG
{
    int d;
    char* s;
    double f;
};

int main()
{
    printf("%d %s %f \n", 1, "two", 3.1415 );
    // Output: 1 two 3.141500

    char format[ 1024 ] = "%d %s %f\n";
    ARG args[ 5 ] = { };
    args[ 0 ].d = 1;
    args[ 1 ].s = "two";
    args[ 2 ].f = 3.1415;
    printf( format, args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], args[ 4 ] );
    // Output: 1 two 3.141500

    return 0;
}

Some things you'll note about my solution:

  • No attempt is made to produce the correct number of arguments. i.e. I oversupply the arguments, but, most functions will look at the first parameter to determine how to handle the rest (i.e. format)
  • I didn't bother dynamically create the format, but, it is a trivial exercise to build a routine that dynamically populates format and args.

Tested this on: - Ubuntu, g++ - Android NDK

I did some more testing, and, confirmed @PeterCoordes comments about this answer not working for double precision.

Related