csp@gtenmc.UUCP (Charudutta S. Palkar) (07/28/90)
This is to avoid the usage of varargs. The only restriction is to pass number of arguments as the first parameter. Will this piece of code execute irrespective of the order in which the parameters are pushed on the stack ie. right to left or left to right? This is executable on Unix V.3 on a VAX-8000. void ftest(n) int n; { char **p; p = (char **)( &n + 1 ); while( n-- ) printf("%s",*p++); } main() { ftest(2,"Rama ","Sita "); ftest(4,"\n","Xxxx ","Sita ","\n"); }
henry@zoo.toronto.edu (Henry Spencer) (07/28/90)
In article <831@gtenmc.UUCP> csp@gtenmc.UUCP (Chardutta S. Palkar) writes: > This is to avoid the usage of varargs. The only restriction > is to pass number of arguments as the first parameter. > Will this piece of code execute irrespective of the order > in which the parameters are pushed on the stack ie. right > to left or left to right? ... No. You can forget about finding a portable way to avoid using vararg.h (or its improved ANSI incarnation, stdarg.h); there is *none*. None. NONE. ***NONE***. There are a dozen different tricks people are fond of using, and they all fail on an increasing number of machines. (What makes you think the parameters are pushed on the stack in any simple order, or indeed that they are pushed onto a stack at all?) -- NFS: all the nice semantics of MSDOS, | Henry Spencer at U of Toronto Zoology and its performance and security too. | henry@zoo.toronto.edu utzoo!henry
steve@taumet.com (Stephen Clamage) (07/29/90)
csp@gtenmc.UUCP (Charudutta S. Palkar) writes: > This is to avoid the usage of varargs. The only restriction > is to pass number of arguments as the first parameter. > Will this piece of code execute irrespective of the order > in which the parameters are pushed on the stack ie. right > to left or left to right? [ included code which scanned up the stack from first parameter ] 1. Some systems do not necessarily pass parameters on a stack. To get variable parameter lists, you need help from the compiler. 2. On some systems the stack grows up, on some it grows down. Some systems push parameters left-to-right, others right-to-left. Combining these two unknowns, you might scan up OR down from the first parameter to find the rest. 3. Some systems use differing alignments when pushing arguments on the stack, and a simple scan will fail. Some Unix systems added <varargs.h>, and ANSI standardized on <stdarg.h>, so that you could write code that would always work, no matter how the compiler or implementation changed. If you can't or won't use these aids, you will have to check each release of each compiler and each runtime system to see whether the code you write will work. -- Steve Clamage, TauMetric Corp, steve@taumet.com
tkacik@kyzyl.mi.org (Tom Tkacik) (07/29/90)
In article <831@gtenmc.UUCP>, csp@gtenmc.UUCP (Charudutta S. Palkar) writes: > Will this piece of code execute irrespective of the order > in which the parameters are pushed on the stack ie. right > to left or left to right? This is executable on Unix V.3 > on a VAX-8000. > void ftest(n) > int n; > { > char **p; > p = (char **)( &n + 1 ); > while( n-- ) > printf("%s",*p++); > } This is not portable. It requires that the stack grow down. Some machines (eg. AT&T WE32000, used in 3b2's) have a stack that grows up. Others pass the first few parameters in registers. To be portable, use varargs. -- Tom Tkacik tkacik@kyzyl.mi.org Speaking only for myself here in Royal Oak.