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?
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.
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!!! *
I'll now go hang my head in shame for thinking of such an idea.
...hmmm...maybe not portable...for sure not nice...but may solve yor problem...
If the number of elements in the list is limited, I would go for manual dispatch depending on the number of elements.
<just for fun>
</just for fun>
What you are trying to use is
alloca
. Ava_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:
To allocate memory on the stack, without having to write a variadic functions use
alloca
. It works more or less likemalloc
, but you don't have to callfree
, it automagically frees itself when you leave the scope.It's fundamentally the same as writing
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: