2

Does anybody know how variable arguments are passed in classic C? I did some debugging today and most regular arguments are passed via stack. However it seems that this does not apply for variable arguments. Are those parameters stored somewhere else like constant strings?

Thanks in advance!

Florian Greinacher
  • 14,478
  • 1
  • 35
  • 53

4 Answers4

3

It depends on the platform. /usr/include/stdarg.h is the place to start looking for details.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • On Unix, yes. On other platforms, `stdarg.h` may be elsewhere. +1, though. – Fred Foo Mar 31 '11 at 19:44
  • While technically true, it is also true that you would have a hard time finding many platforms where they are NOT passed on the stack. – Prof. Falken Mar 31 '11 at 19:48
  • @Amigable: I vaguely recall SPARC doing some interesting things with the register window for varargs, but it's been a while and I don't have access to one any more. – geekosaur Mar 31 '11 at 19:50
  • But Win32 is not this special, is it :) – Florian Greinacher Mar 31 '11 at 19:53
  • 1
    @Florian: see the previous comment, which suggests that some things are passed in registers on x86-64. x86 will be all stack, though: it doesn't have enough registers to even *consider* using them for argument passing. – geekosaur Mar 31 '11 at 19:58
2

They are very often passed on the stack. What you are looking for is ABI specifications for the platform you are using.

For the AMD64 platform, have a look for example here.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
1

You meant Variable-Length Argument Lists?

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

Here's a fun trick

void func(type* values) {
    while(*values) {
        x = *values++;
        /* do whatever with x */
    }
}

func((type[]){val1,val2,val3,val4,0});
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243