|
-
Jan 30th, 2002, 09:58 PM
#1
va_start, va_list, and va_end
Is there anyway to create an argument list (va_list) So that it could be passed into another function???
Like say you had a class which you could retrieve the number of parameters, and the parameter types... Create a va_list... and pass it into another function??
-
Jan 31st, 2002, 05:48 PM
#2
Member
There may be a better solution, but it would be easy to just place all the arguments into one big array and transfer them to the other function that way. As far as different types are concerned, maybe you could create a different array for each type, use a special class, or maybe use void (dangerous!). Someone else may have a better idea though...
-
Feb 1st, 2002, 08:21 AM
#3
va_list is just a typedef for char* on x86
here are the x86 defines for the stdarg macros:
#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define va_end(ap) ( ap = (va_list)0 )
va_start sets the va_list pointer to the last argument + the size of the last argument (= to the next argument)
va_arg returns the dereference of va_list cast to type t and increases va_list by argsizeof(t)
va_end sets va_list to 0.
argsizeof(t) returns the smallest number that is greater than or equal to sizeof(t) and divisible by sizeof(int).
Implementation differs from cpu to cpu, so be careful. Example of creating a va_list on x86:
Code:
#include <stdlib.h>
#include <stdarg.h>
#define argsizeof(t) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
void func(char* fmt, va_list args);
int main()
{
// pass two ints, a short, a char* and a double
va_list list = (va_list)malloc(2*argsizeof(int) + argsizeof(char*) + argsizeof(short) + argsizeof(double));
va_arg(list, int) = 76;
va_arg(list, char*) = "Hello!";
va_arg(list, short) = 2837;
va_arg(list, int) = 0xF40FD0F5;
va_arg(list, double) = 3489.23895;
func("%i%s%is%i%fd", list);
free(list);
return 0;
}
This is not recommended.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|