Mailinglist Archive: opensuse-programming (6 mails)

< Previous Next >
Re: [opensuse-programming] determining the number of args in a vararg call - vscprintf() ?
On Sat, 2011-02-19 at 23:05 +0100, Roger Oberholtzer wrote:
On Sat, 2011-02-19 at 11:21 +0100, Per Jessen wrote:
What is the best/correct way of counting the number of args in vararg
call:

int function( int blah, ... )

I've been googling a bit, and came across mentions of "vscprintf", in
particular this alternative:

#define vscprintf(f,a) vsnprintf(NULL,0,f,a)

More's the pity, since the compiler knows how many
things are on the stack.

The parameters don't really have to be on the stack either. It's
implementation defined where the varargs are sent. va_start() is free to
dynamically allocate memory on the heap for them

There isn't a C standard for how to pass parameters. There is an x86 C
calling convention, an x86_64 C calling convention, a SPARC C calling
convention etc. (think stack vs. registers, for example). The language
leaves that up to the implementation and only specifies how it should
behave from the point of view of the C programs.

The compiler doesn't even always know how large the stack is, between
inline assembly and monstrosities like alloca() anything could happen.
There is no guarantee that the function is called from another C binary.
And on x86, the parameters are in the stack frame belonging to the
previous function technically (before the base pointer, if such a thing
is used), there is no way at all to know how much there is.

The function needs to have some sort of convention, like the first
parameter is the number of parameters, or a format string like printf,
or that the argument list should be terminated by a NULL, or something.
I don't think there is any other way.

Anders

--
To unsubscribe, e-mail: opensuse-programming+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse-programming+help@xxxxxxxxxxxx

< Previous Next >
List Navigation
Follow Ups