Re^2: Variable Parameters

Eric A. Raymond raymond at ptolemy.arc.nasa.gov
Tue Jan 2 11:43:21 AEST 1990


henry at utzoo.uucp (Henry Spencer) writes:

>In article <363 at sixhub.UUCP> davidsen at sixhub.UUCP (bill davidsen) writes:
>The fundamental problem with finding out how many arguments you have --
>ignoring the problem of varying sizes, which got much worse when C
>acquired struct passing -- is that in general you need cooperation
>from the caller.  On some machines, the information can be deduced
>from details of the stack frame or the calling sequence, but on many
>modern systems, it has to be explicitly provided at significant cost.
>Given the call-intensive nature of a lot of C programs, it is a seriously
>bad idea to incur such cost on every function call, given that most
>functions never need the information.

Difficult?  Expensive?  Not really, since at compile time each caller
knows how many arguments it is passed, right?  (Note: this is true
even if the function (i.e., called via a ptr) is unknown at compile-time.)

Assuming every vararg function call / definition exists within the
scope of a prototype, all we need to do is:
  - count the number of (variable) args in each vararg function call
  - pass the number of these args as an extra parameter
  - make some function, say nargs(void), return the value of this
    otherwise unavailable parameter.

Note that the only (runtime) cost is that of passing another argument
when the prototype of the function is declared to use varags.  This is
the only major drawback to this approach, but it's not much to ask
when one considers the benefits.  (Of course, dusty decks out there
may need a bit of help ....).  Even if the function is variable (i.e.,
call via ptr), the use of function prototypes provides us with the
info we need.  (That is, the ptr is strongly typed.)  And of course we
all use pointers.

This might be hard to implement as a macro package. (but why should
language features be limited to what can be expressed in it's macro
language).  Implementing it in a compiler should be fairly straight
forward.

So there's one solution.  A language should not be limitted by the
"that's the way it's been done" factor.

-- 
Eric A. Raymond  (raymond at ptolemy.arc.nasa.gov)
G7 C7 G7 G#7 G7 G+13 C7 GM7 Am7 Bm7 Bd7 Am7 C7 Do13 G7 C7 G7 D+13: Elmore James



More information about the Comp.lang.c mailing list