passing variable numbers of arguments

Karl Heuer karl at haddock.ima.isc.com
Sat Jan 14 18:12:23 AEST 1989


In article <9358 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <11410 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
>>... given this information, the code ... is portable.
>
>No it isn't!!  ... your use of sizeof to adjust the byte count is flawed,
>because sizeof doesn't take into account alignment requirements of the
>arguments, which can (and sometimes do) differ from those of array members.

I'm assuming that va_nbytes() would be *defined* to return sum(each undeclared
argument a)(sizeof(a)), rather than the amount of space used on the stack.  On
implementations where these agree, there may be a fast way to compute the
value from the callee.  On implementations with the strange alignment
requirements, or where other problems prevent the callee from doing this, the
compiler would have to provide the information at the caller's end.

Actually, both va_nbytes() and va_nargs() are more powerful than necessary.
Typically, all the user wants to know is whether or not there is another
argument available for fetching with va_arg(), so a boolean va_isarg() would
suffice.  Any implementation on which it is currently possible to write either
va_nbytes() or va_nargs() correctly could add va_isarg() in such a way that
there is no cost associated with it in programs that don't use it.  (There are
implementations in which none of these three can be written without passing
hints from the caller, which is probably the main reason why this isn't worth
standardizing.)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list