alloca problem?

Paul De Bra debra at alice.UUCP
Sun Nov 6 04:26:29 AEST 1988


In article <17026 at santra.UUCP> hsu at santra.UUCP (Heikki Suonsivu) writes:
]Why this fails,
]
]huu(name)
]	char *name;
]{
]	char *d;
]
]	d = strcpy(alloca(strlen(name) + 1), name);
]
]	....
]}
]
]but this works?
]
]huu(name)
]	char *name;
]{
]	char *d;
]
]	d = alloca(strlen(name) + 1);
]	strcpy(d, name);
]
]	....
]}
]
This is not a problem of your Unix but of your program. Alloca does some
messing around with the stack. When you call it while you are pushing
arguments on the stack, you get in trouble. What I think is happening is
that name gets pushed on the stack, and then the alloca call moves the
stack pointer causing strcpy to go look for "name" in a different place.

The second example works because you first modify the stack and then
start pushing the arguments on the stack.
I am not saying that the first example won't work on any system (that has
alloca) but is is bad practice.

Paul.
-- 
------------------------------------------------------
|debra at research.att.com   | uunet!research!debra     |
------------------------------------------------------



More information about the Comp.unix.wizards mailing list