Variable Parameters

Henry Spencer henry at utzoo.uucp
Mon Jan 1 11:49:14 AEST 1990


In article <363 at sixhub.UUCP> davidsen at sixhub.UUCP (bill davidsen) writes:
>| Your function has to have some way of knowing how many arguments there
>| are, or which is the last argument.  
>... If a portable varargs had been designed in at the start of the
>language it would have saved a lot of future problems. Even nargs()
>would help. I wonder if it was decided to leave it out of C, or if it
>by oversight just never got in.

Actually nargs() existed in early implementations, although it was always
somewhat defective in that it reported the number of words of arguments
rather than the number of arguments.  (In BCPL the two numbers are always
the same, but not in C.)  It departed due to growing implementation
difficulties.

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.

Eventually there was enough demand for somebody at Bell Labs (Dennis?)
to invent <varargs.h>.  (No, like so many other things credited to
Berkeley because they don't put credits in their manual pages, it was
*not* invented at Berkeley.  It existed in V7, although it was not
documented.)  X3J11 ended up fiddling with it a bit to make it more
portable, but the facility finally does exist officially.  However,
you *still* need to have some way of figuring out how many arguments
you've got -- the varargs stuff won't tell you.  Given that it is now
not kosher to use a varargs function without a prototype in scope, I
suppose one could mandate a way to find out the size of the argument
list, but the problem of differing sizes remains.
-- 
1972: Saturn V #15 flight-ready|     Henry Spencer at U of Toronto Zoology
1989: birds nesting in engines | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list