Grabbing "n" arguments in a function

Michael Meissner meissner at osf.org
Fri May 4 01:24:39 AEST 1990


In article <347 at brontolo.sublink.ORG> peter at brontolo.sublink.ORG
(Network administrator) writes:

| In article <297 at ndla.UUCP>, platt at ndla.UUCP (Daniel E. Platt) writes:
| ] In article <559 at dplace.UUCP>, djl at dplace.UUCP (Dave Lampe) writes:
| ] > rick at tmiuv0.uucp writes:
| ] > 
| ] > >What's the most portable way for a function to receive an arbitrary list of
| ] > >arguments?  The function must receive "n" arguments, all of which are char.
| ] > >pointers:
| ] > 
| ] > >    char *weirdfunc(char *arg1, char *arg2, ... char *argn) {
| ] > 
| ] ...In your case 
| ] you'ld continue popping the things from the stack until you got a NULL.
| 
| Dan's method to get arguments from the function's stack is very usefull,
| and is worth ok on every OS and C compiler I have tried, exept for the
| DEC's RISC one.
| 
| Does anybody out there know how to do the same variable argument call
| (WITHOUT "varargs", please! [it doesn't work, too!!!!!]) under MIPS
| RISC workstation (or DEC's one) ?????????
| Thank you

By the way, it probably won't work for any other implementation that
passes arguments in registers (including most RISC processors, like
the 88k).  For machines that pass things in registers, the compiler
has to store any registers normally used for passing arguments for
varargs type arguments.  For MIPS-based processors, this means storing
$4-$7 in the appropriate locations on the stack (which the caller must
allocate).  MIPS machines have another fly in the ointment, in that if
the first argument to a function is floating point, it is passed in a
floating point register, and not an integer register, which totally
messes up any varargs function with a floating point number as the
first argument.  For the 88k, it is more complicated, since structures
are passed on the stack (unlike MIPS), so you can't arbitrarily store
the registers in their 'home' location, so you save r2-r7 in a special
8 word area, and use a state variable within the varargs routine to
index through the special 8 word area, or the normal stack argument
area.  Finally, there are machines like the Data General MV, whose
stack grows backwards to most other machines in the universe.

There are good reasons why the varargs/stdarg layer is necessary, and
if your problem can't use varargs/stdarg, you would be well advised to
take some different tack.

--
Michael Meissner	email: meissner at osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA

Catproof is an oxymoron, Childproof is nearly so



More information about the Comp.lang.c mailing list