va_list *



Hello.

void print (va_list *ap)
{
  printf ("%d\n", va_arg (*ap, int));
}

void test (int num, ...)
{
  va_list ap;
  int i;

  va_start (ap, num);
  for (i = 0; i < num; i++)
    print (&ap);
  va_end (ap);
}

int main (void)
{
  test (3, 1, 2, 3);
  return 0;
}

ISO C99 (footnote 215, §7.15) says that this program is valid and should
print out 1, 2, 3...

""
  215) It is permitted to create a pointer to a va_list and pass that
       pointer to another function, in which case the original function
       may make further use of the original list after the other
       function returns.
""

At the same time, though, GObject goes out of its way to avoid passing a
va_list pointer to functions and there was some talk on mailing lists in
the past about this being because not all C implementations are sane
with respect to the above functionality.  See also
http://library.gnome.org/devel/gobject/unstable/gobject-Varargs-Value-Collection.html#G-VALUE-COLLECT:CAPS

""
  Collects a variable argument value from a va_list. We have to
  implement the varargs collection as a macro, because on some systems
  va_list variables cannot be passed by reference.
""

Can anyone please weigh in on whether or not these "some systems" have
gotten their act together yet?  Is it still necessary to go out of our
way like this in glib?

Thanks in advance.

Cheers



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]