stack memory allocation

dan%bbncd at sri-unix.UUCP dan%bbncd at sri-unix.UUCP
Wed Jul 6 05:31:00 AEST 1983


From:  Dan Franklin <dan at bbncd>

The VAX 4.1BSD C compiler, like many C compilers, pushes arguments on the stack
as they are generated for a function call.  Thus, salloc() will not work in:
	foo(arg1, salloc(N), arg3);
Arg3 gets pushed on the stack, then salloc() is called and its return value is
pushed, then arg1, and finally foo() is called.  But arg3 is far away from the
other arguments; foo() will never find it!

I couldn't make the VAX compiler use the stack for expression analysis; I
probably didn't make it complicated enough.  This is a common strategy, though,
and if you think you might run the code on another system someday you should
avoid it.  Simple assignment statements would probably work:
        p = salloc(N);
on machines like the VAX and PDP-11.  On at least one architecture I know of,
though, the compiler generates ALL references to local vars off sp; thus
salloc() would fail rather spectacularly no matter how you used it.  Also, the
optimizer is no respecter of statement boundaries, so maybe even doing an
salloc() NEAR some function calls or complex expressions could screw up.

Personally, I think salloc() is too implementation-dependent to be worth it,
even though it would be incredibly useful.

	Dan Franklin



More information about the Comp.unix.wizards mailing list