Function with variable number of args
Henry Spencer
henry at utzoo.UUCP
Sat Jun 30 15:47:37 AEST 1984
>I was amazed to read this statement in a recent post:
>
> There is *no* fully machine-independent way to write a
> function which takes a variable number of arguments, even
> if all the function is trying to do is to pass its argument
> list on to another function.
>
>The language BCPL has precisely this feature, and as far as I know
>there has been no major problem with it in 20 years. The way to
>write the function is to declare it with the maximum number of
>arguments, and you can scan the argument list sequentially by
>simply building a pointer to the first argument and stepping it:
>
> ptr := LV arg1
> ...
> ptr := ptr + (LV arg2 - LV arg1)
>
>(LV in BCPL is & in C). The language semantics requires the args to be
>stored in consecutive cells, though they may run upwards or downwards in
>memory, hence the above code. I've never heard of any implementation
>difficulties, and have never encountered any myself in the BCPL ports
>I've done.
For one thing, C is not BCPL, and C does *not* require consecutive cell
allocation of arguments. So you can't (in principle) depend on that.
There are C compilers that put some of the arguments in registers, for
example. (Please don't tell me that these compilers run into trouble
with various things; I'm quite aware of this. But they do exist.)
For another thing, while I respect your BCPL porting experience, I would
say that you've never tried to implement BCPL on really difficult machines.
There exist machines -- with Unix implementations running on them -- which
absolutely require that the size of the argument list of a given function
be a compile-time constant, as a fundamental assumption of their only call
primitive. Such machines break *all* the simplistic variable-arguments
techniques; really heavy kludging is required to make things work.
--
Henry Spencer @ U of Toronto Zoology
{allegra,ihnp4,linus,decvax}!utzoo!henry
More information about the Comp.unix.wizards
mailing list