Variable argument lists.
Ken Raeburn
raeburn at athena.mit.edu
Fri May 20 13:24:45 AEST 1988
In article <5485 at aw.sei.cmu.edu> firth at bd.sei.cmu.edu.UUCP (Robert Firth) writes:
>In article <1777 at rpp386.UUCP> jfh at rpp386.UUCP (The Beach Bum) writes:
>
>[ why can't functions with variable numbers of arguments determine the
> number of arguments they were called with? ]
>
>>could someone please present an example of an architecture were this
>>CAN'T be done? and does anyone actually have timings for a system where
>>this has been done versus not to support the claim that doing this is
>>very expensive?
>
> [....]
>
>If we consider the average function has two parameters, then the extra
>cost is a (hidden) third parameter. That raises the full call/entry/
>exit/return sequence from ~10 instructions to ~12, ie an overhead of 20%.
>
>No, I do not want to pay that price.
How about the average number of arguments passed to variadic
functions? They're the only ones that should need the information.
Yes, I realize that includes *printf, some of the most-used functions.
(And *scanf too, but who in their right mind uses those anyways? :->)
And the work that they do quite likely far outways the cost of an
extra argument, especially since that argument is going to be a
constant known at compile-time. (Well, okay, I suppose anything's
possible, and I'm not familiar with too many architectures, but are
there [m]any out there that would lose big here?)
I wouldn't mind a very slight extra cost for runtime code to catch
that missing argument to printf when I goof up.
And for those who do insist on using *scanf, it's probably of greater
importance. While printf might by chance pick up some stack garbage
or random stuff in data space, or could die by reaching outside the
address space, scanf will probably cause trouble by overwriting the
same information.
Most of the variadic functions I've seen outside of the C library are
routines that print an error message (via fprintf/_doprnt, yes I'm
using BSD) and exit with an error status. This means that the number
of arguments winds up being ignored, though if it were available
through the language (and some v*printf interface permitted it) it
could still be used to check the argument list against the format
string. The (machine-specific) definition of va_arg could even be
written to note immediately when the end of the argument list has been
reached (if the dpANS permits enough leeway in that particular case).
- Ken Raeburn
MIT Project Athena
More information about the Comp.lang.c
mailing list