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)
I am not sure you can count them. The called function really has no way to know how many things have been put on the call stack. vsprinf parses the format string and, really, hopes for the best. This is one cause of segmentation violations. The only thing the compiler can do, after a function call, is to set the stack back to the same location as before the call. But is does not pass to the function how many items were put on the call stack. More's the pity, since the compiler knows how many things are on the stack. But the function interface design (C calling convention) does not provide a mechanism for knowing this. I guess other languages like C# might have this mechanism. Not sure. But if I wa to design a new language and function call convention, I would add a count and per-item type spec to the call. Sure would make programming less a crap shoot! Having said all this, if there was a way I would like to know what it is. I guess stack checking code that can be enabled when compiling must do something here. But I think it is hidden from the called function.
/Per Jessen, Zürich
-- Roger Oberholtzer OPQ Systems / Ramböll RST Ramböll Sverige AB Krukmakargatan 21 P.O. Box 17009 SE-104 62 Stockholm, Sweden Office: Int +46 10-615 60 20 Mobile: Int +46 70-815 1696 SHAW'S PRINCIPAL Build a system that even a fool can use, and only a fool will want to use it. -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org