Create va_list dynamically

2020-02-01 00:14发布

I have a function

void foo(int cnt, va_list ap);

I need to use it, but requirement is quite strict, number of va_list vary and it will change during run-time. What I would like to do is:

create a va_list (which expects char*) form

QList<Contact*>

where Contact is a defined class

class Contact
{
   public:
      QString getName();
   private: 
      QString m_name;

}; 

and I would like to populate in the loop va_list for example:

for (int idx = 0; idx<contacts.count(); idx++)
{
    contacts.at(idx)->getName(); // this i would like to pass to va_list

}

Does anybody have a clue about how I could do this?

7条回答
Luminary・发光体
2楼-- · 2020-02-01 00:57

It depends on compiler what is the va_list type, what are the va_start and va_end macros. You cannot do this in a standard way. You would have to use compiler-specific construction.

Maybe you can alter the 'foo' function? If so, then make it inversely - convert va_list to QList and make 'foo' accept QList.

// EDIT

Then see what the va_list type is, what the va_start and va_end macros are in your specific compiler. Then build your va_list in such a way that these macros will work on it.

查看更多
狗以群分
3楼-- · 2020-02-01 00:59

What you're wanting to do is to simulate the call stack so you can pass a constructed va_list to foo(). This is rather specific to the compiler ( and warning, there are differences between even 32- and 64-bit compilers ). The following code is for ENTERTAINMENT PURPOSES ONLY!!! as (if it even works on your system) it is prone to breakage. With it, I use a flat memory buffer and the populate it with a count and a bunch of character strings. You could fill it as appropriate with pointers to your strings and hand them down.

It does seem to work on my system, Windows 7 w/ Visual Studio 2008, for 32-bit applications only.

* BAD IDEA CODE FOLLOWS!!! *

#define PSEUDOSTACKSIZE ( sizeof(int) + 999 * sizeof(const char*) )
#pragma pack( push,1 )
union PSEUDOSTACK
{
    int count;
    char data[PSEUDOSTACKSIZE];
};
#pragma pack( pop )

void foo( int count, va_list args )
{
    for ( int i = 0; i < count; i++ )
    {
        char *s = va_arg( args, char* );
        printf( "%s\n", s);
    }
}

void bar( PSEUDOSTACK data, ... ) 
{ 
    va_list args; 
    va_start(args, data.count); 
    foo( data.count, args);
    va_end(args); 
} 
// And later on, the actual test case code.
PSEUDOSTACK barData;
barData.count = 999;
char *p = barData.data + sizeof(int);
for ( int i = 0; i < 999; i++, p += sizeof(char*) )
{
    *reinterpret_cast<char**>(p) = "ThisIsABadIdea";
}
bar( barData );

I'll now go hang my head in shame for thinking of such an idea.

查看更多
手持菜刀,她持情操
4楼-- · 2020-02-01 00:59

...hmmm...maybe not portable...for sure not nice...but may solve yor problem...

  • va_list is (at least for visual c++) just a #define for char*
  • → arguments don't need to be on the stack
  • → arguments are just required to be continuous in memory
  • → no need to use assembler and/or copying (see my 'just for fun answer' :-)
  • → no need to worry about cleanup
  • efficient!
  • tested on w2k3 sp2 32bit + vc++ 2010

#include <stdarg.h>
#include <string>
#include <vector>
#include <iostream>

#define N 6 // test argument count

void foo(int n, va_list args);

int main(int, char*[])
{
    std::vector<std::wstring> strings;
    std::wstring s(L"a");
    int i(0);

    // create unique strings...
    for (; i != N; ++i)
    {
        strings.push_back(s);
        ++s.front();
    }
    foo(N, reinterpret_cast<va_list>(strings.data()));
    return 0;
}

void foo(int n, va_list args)
{
    int i(0);

    for (; i != n; ++i)
        std::wcout << va_arg(args, std::wstring) << std::endl;
}

查看更多
放我归山
5楼-- · 2020-02-01 01:08

If the number of elements in the list is limited, I would go for manual dispatch depending on the number of elements.

void call_foo(int count, ...) {
    va_list args;
    va_start(args, count);
    foo(count, args);
    va_end(args);
}

switch (contacts.count()) {
    case 0: return call_foo(contacts.count());
    case 1: return call_foo(contacts.count(),
                            contacts.at(0)->getName());
    case 2: return call_foo(contacts.count(),
                            contacts.at(0)->getName(),
                            contacts.at(1)->getName());
    case 3: return call_foo(contacts.count(),
                            contacts.at(0)->getName(),
                            contacts.at(1)->getName(),
                            contacts.at(2)->getName());
    default: /* ERROR HERE, ADD MORE CASES */ return call_foo(0);
}
查看更多
仙女界的扛把子
6楼-- · 2020-02-01 01:10

<just for fun>

  • allowing arbitrary argument count
  • luckily sizeof(std::wstring) is a multiple of sizeof(int)
  • tested on w2k3 sp2 32bit + visual c++ 2010

#include <stdarg.h>
#include <string>
#include <vector>
#include <iostream>

#define N 6 // test argument count

void foo(int n, ...);

int main(int, char*[])
{
    std::vector strings;
    std::wstring s(L"a");
    int i(0);

    // create unique strings...
    for (; i != N; ++i)
    {
        strings.push_back(s);
        ++s.front();
    }

    int n_stack_strings(N*sizeof(std::wstring)),    // space needed for strings
        n_stack(sizeof(int)+n_stack_strings);   // overall stack space...needed for cleanup

    __asm sub esp, n_stack_strings  ; reserve stack space

    std::wstring* p_stack(0);

    __asm mov p_stack, esp  ; get stack pointer

    std::wstring* p(p_stack);
    std::vector<std::wstring>::iterator string(strings.begin());

    // copy to stack
    for (; string != strings.end(); ++string, ++p)
        new (p) std::wstring(*string);
    __asm push N    ; argument count...arguments right to left (__cdecl)
    __asm call foo
    // cleanup
    for (p = p_stack; p != p_stack+N; ++p)
        p->~basic_string();
    __asm add esp, n_stack  ; caller has to cleanup the stack (__cdecl)
    return 0;
}

void foo(int n, ...)
{
    int i(0);
    va_list marker;

    va_start(marker, n);
    for (; i != n; ++i)
        std::wcout << va_arg(marker, std::wstring) << std::endl;
    va_end(marker);
}

</just for fun>

查看更多
够拽才男人
7楼-- · 2020-02-01 01:14

What you are trying to use is alloca. A va_list object can not store variables, the function call stores them, and you can only access it via va_list. These variables are only valid during the call, and they get ovverwriten afterwards.

THIS WILL NOT WORK:

va_list func(int dummy, ...)
{
   va_list result;
   va_start(result, dummy);
   return result;
}

To allocate memory on the stack, without having to write a variadic functions use alloca. It works more or less like malloc, but you don't have to call free, it automagically frees itself when you leave the scope.

int * local = (int *) alloca(3 * sizeof(int));
local[0] = 10;
local[1] = 20;
local[2] = 30;

It's fundamentally the same as writing

int local[3];
local[0] = 10;
local[1] = 20;
local[2] = 30;

But with alloca 3 does not need to be a constant. Again you can only use it inside the enclosing scope, so do not return it from the function.

if what you want from a va_list is the multiple types in one list consider writing a union like this:

union variant
{
    int          i;
    unsigned int u;
    float        f;
    double       d;
    const char * s;
    void *       v;
};
查看更多
登录 后发表回答