I use the ffcall library (in particular, the avcall package ffcall) to dynamically enter parameters into variable functions. those. we have
int blah (char *a, int b, double c, ...);
and we want to call this function with values โโtaken from the user. To do this, we create an avcall version of the function:
int av_blah (char *a, int b, double c, char **values, int num_of_values)
{
av_alist alist;
int i, ret;
av_start_int (alist, &blah, &ret);
av_ptr (alist, char*, a);
av_int (alist, b);
av_double (alist, c);
for (i=0;i<num_of_values;i++)
{
}
av_call (alist);
return (ret);
}
Now I am using avcall with:
int read_row (struct some_struct *a, struct another_struct *b[], ...);
And it is used like this:
struct some_struct a;
struct another_struct **b = fill_with_stuff ();
char name[64];
int num;
while (read_row (&a, b, name, &num)==0)
{
printf ("name=%s, num=%d\n", name, num);
}
But I want to use avcall to capture a certain number of values โโfrom this function, and I do not know this information in advance. So I thought I was just creating an array of void pointers, and then malloc space according to type:
char printf_string[64]="";
void **vals = Calloc (n+1, sizeof (void*));
while (av_read_row (&a, b, vals, n, printf_string) == 0)
{
av_printf (printf_string, vals, n);
void **ptrs = vals;
while (*ptrs) {
free (*ptrs);
*ptrs=NULL;
ptrs++;
}
printf_string[0]='\0';
printf ("\n");
}
And av_read_rowrightly so:
int av_read_row (struct some_struct *a, struct another_struct *b[], void **vals, int num_of_args, char *printf_string)
{
int i, ret;
av_alist alist;
av_start_int (alist, &read_row, &ret);
av_ptr (alist, struct some_struct *, a);
av_ptr (alist, struct another_struct **, b);
for (i=0;i<num_of_args;i++)
{
switch (type)
{
case INT: {
vals[i] = Malloc (sizeof (int));
av_ptr (alist, int*, vals[i]);
strcat (printf_string, "%d, ");
break;
}
case FLOAT: {
}
}
}
av_call (alist);
return (ret);
}
, , , . , , ? , mallocs av_read_row while. - , , -?