delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2000/06/20/12:00:13

Message-ID: <394F94BB.67B7C80E@softhome.net>
Date: Tue, 20 Jun 2000 17:58:51 +0200
From: Laurynas Biveinis <lauras AT softhome DOT net>
X-Mailer: Mozilla 4.73 [en] (Win98; U)
X-Accept-Language: lt,en
MIME-Version: 1.0
To: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
CC: DJGPP Workers <djgpp-workers AT delorie DOT com>
Subject: Re: Patch: new GCC builtins for stdarg.h/varargs.h
References: <Pine DOT SUN DOT 3 DOT 91 DOT 1000619200929 DOT 28843A-100000 AT is>
Reply-To: djgpp-workers AT delorie DOT com

Eli Zaretskii wrote:
> If you can run the trivial implementation via some test suite that proves
> that it indeed works, then I agree.

How about following?
It should print
10
20
24
And it does that with both trivial and builtin va_copy implementation.
What are additional cases which should be tested?

#include <stdarg.h>
#include <stdio.h>

/* At least two additional ints should be passed.  */
/* The last arg should be zero.  */
void magic_sum(int first, ...)
{
   va_list args;
   va_list arg_copy;
   int i, sum = first;
   va_start(args, first);
   /* Sum up everything twice */
   va_copy(arg_copy, args);
   do
   {
      i = va_arg(args, int);
      sum += i;
   } while (i);
   printf("Now %d\n", sum);
   sum += first;
   do
   {
      i = va_arg(arg_copy, int);
      sum += i;
   } while (i);
   printf("Now %d\n", sum);
   /* Add everything one more time except first three args. */
   va_start(arg_copy, first);
   va_arg(arg_copy, int);
   va_arg(arg_copy, int);
   va_copy(args, arg_copy);
   do
   {
      i = va_arg(arg_copy, int);
      sum += i;
   } while (i);
   printf("And finally %d\n", sum);
   va_end(args);
   va_end(arg_copy);
}

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019