Path: utzoo!attcan!uunet!lll-winken!lll-ncis!helios.ee.lbl.gov!nosc!ucsd!ucbvax!decwrl!decvax!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: passing variable numbers of arguments Message-ID: <11410@haddock.ima.isc.com> Date: 12 Jan 89 22:13:30 GMT References: <899@thor.stolaf.edu> <15341@mimsy.UUCP> <449@marob.MASA.COM> <9317@smoke.BRL.MIL> <11378@haddock.ima.isc.com> <9321@smoke.BRL.MIL> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Distribution: na Organization: Interactive Systems, Boston Lines: 40 In article <9321@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes: >In article <11378@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes: >>[returning the total number of varargs bytes might be useful] > >No, if nargs() has any use at all it cannot be this. The reason that >the va_ stuff must be used for portable access to arguments is that >many architectures do not have a simple, clean model for argument >alignment (which might be different from that indicated by sizeof), >and some pass some of the arguments different from others. nbytes() >would be of no value in portable programming. I don't see any problem. The proposal is that the variadic function knows the sum of the sizes of the (widened) arguments in the nonfixed portion of the arglist; given this information, the code nb = va_nbytes(); while (format_indicates_another_arg) { if (char_expected) { nb -= sizeof(int); ch = (char)va_arg(ap, int); do_char(ch); } else if (long_expected) { nb -= sizeof(long); ii = va_arg(ap, int); do_long(ii); } } is portable. (To be useful, add appropriate checks on the value of nb.) If the calling convention is such that the callee can compute the byte count, then everything is okay. If not, then the caller (which can easily compute it) can supply it via a hidden argument. It doesn't matter how kludgy the argument passing mechanism is. I'm not trying to claim that this would solve all of the problems with variadic functions; this is just to demonstrate that it's not necessarily fatal that the pdp11 nargs() can't count actual arguments. Either a true arg count or a byte count could plausibly be standardized (the caller knows both), but on some implementations the byte count is already available without an extra hook. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint