passing variable numbers of arguments

Karl Heuer karl at haddock.ima.isc.com
Tue Jan 10 05:23:13 AEST 1989


I actually (slightly reluctantly) agree that it's not worthwhile to provide
nargs() or equivalent, but allow me to play Devil's Advocate for a bit...

In article <9317 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <449 at marob.MASA.COM> samperi at marob.masa.com (Dominick Samperi) writes:
>>It also seems to be the case that it is not possible for a called function
>>(with a variable number of args) to determine how many args were actually
>>passed, or when the last arg has been fetched, unless this information is
>>supplied in the first parameter, say.
>
>Yup.  This reflects existing C practice.  PDP-11 UNIX had an nargs()
>function that purported to return the argument count, but it didn't
>work right (it returned the number of words of arguments, not the count).
>Making this work right would have slowed down every function call.

Only calls to variadic functions; fixed-argument functions don't need the
feature, and it needn't be supplied for them.  (An ANSI compiler can tell
whether a function is variadic.)  Moreover, since it removes the need for a
sentinel on such functions as execl(), the cost of adding it as a pseudo-
argument should be offset by the benefit of removing the sentinel.  (And on
some implementations, the cost is already zero, so the benefit would be picked
up for free.)

(Lest someone feel the need to point this out: the major problem is that
execl() is already frozen; also, the printf() family doesn't pick up the
benefit, since there's no sentinel.)

>>Wouldn't it have been reasonable for the standard to specify [nargs]?
>
>Why burden all implementations all the time when you can provide this
>yourself as a patent argument in those cases where you need it?

Because computers are much better at counting things than humans are.  If some
of the arguments are ifdef'd out, getting the count right can be hairy.

>You could also use a "sentinel" argument, e.g.
>	concat(dest,"str_a","str_b","str_c",(char*)0);

This works as long as the arg type is something with an out-of-band value, but
it doesn't help if you want to write a function min(int, ...).

Chris Torek wrote:
>Possibly.  But that is not enough information: one needs not only the
>number of parameters, but also their types.  It seems to me that doing
>half the job (providing the count without the types) would be worse than
>doing nothing at all.

Yes, it does seem as though nargs() is not very useful for polymorphic
functions.  (And, as Doug pointed out, the number-of-words implementation is
bad--though this could be fixed by having nargs() return the number of bytes
so that the application could say nb -= sizeof(TYPE), assuming it knows what
type to expect next.)

Perhaps what we need is a separate type of declaration syntax for variadic,
automatically-argcounted, monomorphic functions; then only those functions
that use this new declaration would pay the price, if any, associated with
making the information available.

Fix it in `D'...

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