Expert C Programming

(Jeff_L) #1

the stack grow by calling a function, and declaring some large local arrays.


What's the address of the top of the stack now?


The stack may be located at a different address on different architectures and for different OS
revisions. Although we talk about the top of the stack, the stack grows downwards on most processors,
towards memory addresses with lower values.


What Happens When a Function Gets Called: The Procedure


Activation Record


This section describes how the C runtime manages the program within its own address space. Actually,
the runtime routines for C are remarkably few and lightweight. In contrast to, say, C++ or Ada, if a C
program wants some service such as dynamic storage allocation, it usually has to ask for it explicitly.
This makes C a very efficient language, but it does place an extra burden on the programmer.


One of the services that is provided automatically is keeping track of the call chain—which routines
have called which others, and where control will pass back to, on the next "return" statement. The
classic mechanism that takes care of this is the procedure activation record on the stack. There will be
a procedure activation record (or its equivalent) for each call statement executed. The procedure
activation record is a data structure that sup-ports an invocation of a procedure, and also records
everything needed to get back to where you came from before the call. (See Figure 6-4.)


Figure 6-4. A Canonical Procedure Activation Record

The description of the contents of activation records is illustrative. The exact structure will vary from
implementation to implementation. The order of the fields may be quite different. There may be an
area for saving register values before making the call. The include file,


/usr/include/sys/frame.h, shows how a stack frame looks on your UNIX system. On


SPARC, a stack frame is large—several dozen words in size—because it provides room to save
register windows. On the x86 architecture, the frame is somewhat smaller. The runtime maintains a

Free download pdf