Unix Stack Frame Questions

Lee Derbenwick lfd at cbnewsm.att.com
Fri Apr 5 09:07:16 AEST 1991


In article <3465 at unisoft.UUCP>, greywolf at unisoft.UUCP (The Grey Wolf) writes:
> /* <GOEHRING.91Mar25113709 at gnu.ai.mit.edu> by goehring at gnu.ai.mit.edu
>  * In article <125 at epic.epic.com> tan at epic.epic.com (Andy Tan) writes:
>  * 
>  *    1. Is it right to assume that the address of the last automatic
>  *       variable is the bottom of stack frame ?
>  * 
>  * it is not right to assume that there is a stack frame, and some
>  * compilers aren't going to put autos in the frame even if a frame
>  * exists since they can be more cheaply handled with registers.
> 
> If, of course, you have the registers (68K only have so many).
> If there's not a stack frame, how are parameters passed to the
> function...?  And how would you return...?

Even if you know that you have a real stack, some processors grow stacks
up from low address towards higher addresses, and some grow them from
higher addresses toward lower ones.

And some compilers put the first automatic (non-register) variable at the
top of the stack frame, and some put the last auto variable at the top.

So even if you have a real, contiguous stack, knowing the address of one
auto variable doesn't tell you anything portable.

And there are probably some processors that don't maintain a physical
stack at all -- they do the equivalent of a malloc for each stack frame,
and keep a pointer to the calling function's frame.  So while each
function may have a stack frame (or a collection of them, depending how
much can be allocated in one chunk), the actual stack is a linked list.

(Don't laugh!  This is how recursive PL/I procedures worked on the IBM
System/360 and 370; I wouldn't be surprised if the C compilers for their
descendants still do, since IBM had a _very_ standardized calling
sequence for all languages.  Non-recursive procedures generally allocated
their stack frames statically, but all C functions are theoretically
recursive, so I wouldn't expect static allocation to be used except by a
_highly_ optimizing compiler that does inter-procedural control flow
checks.)

All the C language guarantees you is behavior _as if_ there were a
stack.  Regardless of whether you're running it on a Unix system, some
other OS, or bare hardware...

 -- Speaking strictly for myself,
 --   Lee Derbenwick, AT&T Bell Laboratories, Warren, NJ
 --   lfd at cbnewsm.ATT.COM  or  <wherever>!att!cbnewsm!lfd



More information about the Comp.unix.programmer mailing list