Variable length argument list handling

Chris Torek chris at mimsy.UUCP
Fri Aug 18 23:26:52 AEST 1989


In article <1441 at esunix.UUCP> bjones at esunix.UUCP (Ben Jones) writes
[in carefully right-adjusted text, which is then hard to read; I
have deleted the extra right-adjusting spaces]:
>Question:  Why can't a C function which allows a variable number of
>arguments find out how many arguments were actually passed?

This proposal comes up at least once a year.

The answer is: (a) no one does it now, and (b) it is not terribly
useful, so it has not been standardised.

>The VAX/VMS version of "varargs.h" provides for a macro called
>"va_count" which obtains the number of longwords in the argument list.
>This is not quite a parameter count but it is probably the best that
>can be done since parameter lists can have mixed integer (one
>longword) and floating point (two longwords) arguments.

This demonstrates one of the basic problems with `how many arguments
did I get?': it does not answer the question `what kind of arguments
did I get?'.  In this case, the two answers are mixed together (shaken,
not stirred) and about 1/2 of the result is poured out.

>In any case, this feature is available on the VAX because the
>procedure calling standard requires a parameter list to start with the
>number of longwords.  In fact, the CALLS instruction uses this standard
>to pop the parameter list off the stack automatically when the
>procedure returns.

This longword-count is, however, restricted to a maximum of 255.
Thus, if I write

	struct huge { int a[10000]; } h;
	...
	var_args_function(h);

the va_count function will probably return (10000%256), or 16.  Not
what I had in mind!  The 4BSD PCC correctly pops the ten thousand
longwords off the stack on return, and VAX gcc does as well.  Gcc, in
fact, goes so far as to use 0 as the count of longwords when the count
overflows, which would lead var_args_function to think no arguments
were passed!

>... it would appear that the best way to get the length of the argument
>list is to add a new reserved word to the language to handle variable
>parameter lists.

If nothing else, you have at least thought quite a bit harder about
the suggestion than most people who post it, because this is in fact
true.

>Just in case nobody has thought of anything better, consider offer the
>following suggestion:

`consider offer'?  `I offer', perhaps?

[definition deleted; see the original article]

Why not go all the way and require the caller to pass the types of each
argument, as well?  This would enable implementations to check the
arguments to printf(), for instance.  (It would have more overhead.)

Note also that the only way to get this in a future standard for a
language that is almost but not entirely unlike C ( :-) ---at least, if
the C languages follow in FORTRAN's footsteps) is to go out and
implement it yourself, and soon.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list