Variable argument lists.

Richard Harter g-rh at cca.CCA.COM
Wed May 11 10:11:21 AEST 1988


In article <11435 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>(Cary Bates) writes:
>>Does anybody know (or care) why in ANSI standard C when 
>>using a variable length argument list, there is no way to 
>>determine how many arguments where passed into the function?

>Simple: it cannot be done on (some|many|most) architectures.

>>In VAX C [by which I presume he means some version of VMS C] there
>>is a macro called va_count.

>I would bet that it does not work.  Try:

>	int f(int x, ...) { return va_count(); }
>	int main() {
>		printf("%d\n", f(0, (double)0, (double)0));
>		return 0;
>	}

>Chances are the program will print either 5 or 4.  Neither is
>correct.

??????

This seems unreasonable -- if such a gismo is part of VAX C (which
is what DEC calls it) and it were my job to implement it, I would
reserve space on the stack for the count and have the compiler
generate code to load the count onto the stack.  The argument count
is available at compile time, after all.  If the gismo returns the
length of the argument section of the stack then it is simply misnamed.

Offhand, this is a reasonable feature -- the cost of loading the count
on the stack in space and time is minor.  The comment that several people
have made that you should pass the count as a separate argument is not
entirely to the point.  One can think of situations where the count
may be unknown, e.g argument list is generated using preprocessor
conditionals.  Also, if you have, for some reason, long argument lists,
sitting down and counting them is a potential source of error.

Mind you, this is no spirited defense of such a feature.  I've done
quite nicely without it, and probably wouldn't use it if were standardly
available (and certainly not if it wasn't completely portable) but it
seems reasonable.
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.



More information about the Comp.lang.c mailing list