rpaul@dolphin.omni.co (Rodian Paul) (05/30/91)
> In article <3599@charon.cwi.nl> jack@cwi.nl (Jack Jansen) writes: > >After some hours of digging through assembly code I've finally found out > >that varargs.h depends on the stack being aligned to an 8-byte boundary, > >and the C compiler does its utmost to keep it like that (unlike me). > > > >Could anyone in the know tell me whether this deduction is correct, and, > >if so, where this is documented? I would expect this to be in the > >'linkage convention' of the Kane book, but it isn't there (or, at > >least, I couldn't find it...) > > kenton@decvax.dec.com (Jeff Kenton) replies: > This is correct. Don't really know where it's documented. I don't know what the original poster was looking for with the var/stdargs alignment question, but here are one or two tricks that I've found work. The following techniques are likely to be non-standard, I haven't tried them on any other platform other than SGI. 1. If you want to pass a structure to a variable arg routine, pass and expect a pointer to the struct. I should think that this is OK regarding alignment because the sizeof a pointer is the sizeof a pointer etc. Because the implementation of the stdarg macros is machine dependant and so is the mapping/padding of structures, I can understand why ANSI couldn't be more precise about the standard. 2. In an old comp.lang.c FAQ, the following question was posed: 46. How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)? A: In general, you cannot. You must provide a version of that other function which accepts a va_list pointer, as does vfprintf in the example above. If the arguments must be passed directly as actual arguments (not indirectly through a va_list pointer) to another function which is itself variadic (for which you do not have the option of creating an alternate, va_list-accepting version) no portable solution is possible. (The problem can be solved by resorting to machine-specific assembly language.) I don't know if my method for accomplishing the above will work on another platform. Once again I use ONLY pointers to the first variadic function, it knows what to expect based on one of it's known args (or a "ghost" variable) and first collects all of the expected arguments (as pointers). You can then pass those args onto another variadic function. This technique helps a lot if you write higher-level library functions, where the library takes most of the burden off of the application programmer. Hope this helps somebody. I'd also be interested to know how dangerous my use of the var/stdarg macros actually is. Cheers. ------------------------------------------------------------------------------- rpaul%crow@ccut.cc.u-tokyo.ac.jp phone: +81 (3) 5706-8357 ccut.cc.u-tokyo.ac.jp!crow!rpaul FAX: +81 (3) 5706-8437