passing variable numbers of arguments

Karl Heuer karl at haddock.ima.isc.com
Fri Jan 13 08:13:30 AEST 1989


In article <9321 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <11378 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
>>[returning the total number of varargs bytes might be useful]
>
>No, if nargs() has any use at all it cannot be this.  The reason that
>the va_ stuff must be used for portable access to arguments is that
>many architectures do not have a simple, clean model for argument
>alignment (which might be different from that indicated by sizeof),
>and some pass some of the arguments different from others.  nbytes()
>would be of no value in portable programming.

I don't see any problem.  The proposal is that the variadic function knows the
sum of the sizes of the (widened) arguments in the nonfixed portion of the
arglist; given this information, the code
	nb = va_nbytes();
	while (format_indicates_another_arg) {
	    if (char_expected) {
		nb -= sizeof(int);
		ch = (char)va_arg(ap, int);
		do_char(ch);
	    } else if (long_expected) {
		nb -= sizeof(long);
		ii = va_arg(ap, int);
		do_long(ii);
	    }
	}
is portable.  (To be useful, add appropriate checks on the value of nb.)  If
the calling convention is such that the callee can compute the byte count,
then everything is okay.  If not, then the caller (which can easily compute
it) can supply it via a hidden argument.  It doesn't matter how kludgy the
argument passing mechanism is.

I'm not trying to claim that this would solve all of the problems with
variadic functions; this is just to demonstrate that it's not necessarily
fatal that the pdp11 nargs() can't count actual arguments.  Either a true arg
count or a byte count could plausibly be standardized (the caller knows both),
but on some implementations the byte count is already available without an
extra hook.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list