[c] Building va_list on heap
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
typedef struct _my_valist
{
unsigned int length;
unsigned int size;
void *data;
void *ptr;
} my_valist;
#define va_add(v, x, t) \
((sizeof(t) <= v.size - v.length) && memcpy((void *)((long)v.data + v.length), &x, sizeof(t)) && (v.length += sizeof(t)) || \
((v.ptr == realloc(v.data, v.size * 2)) && (v.size *= 2) && (v.data = v.ptr) && memcpy((void *)((long)v.data + v.length), &x, sizeof(t))) && (v.length += sizeof(t)))
int main(int argc, char *argv[])
{
int dummy;
my_valist ap;
va_list app;
int myint;
char *mystr = "hello";
ap.data = (char *)malloc(1024);
ap.size = 1024;
ap.length = 0;
if (va_add(ap, mystr, char *)) printf("ok\n");
if (va_add(ap, ap.length, char *)) printf("ok\n");
app = ap.data;
mystr = va_arg(app, char *);
myint = va_arg(app, long);
printf("%s %i\n", mystr, myint);
exit(0);
}
|