Expert C Programming

(Jeff_L) #1

pointer, often in a register and usually called fp, which indicates the active stack frame. This will be


the stack frame nearest to or at the top of the stack.


Software Dogma


Astonishing C Fact!


Most modern algorithmic languages allow functions (as well as data) to be defined inside
functions. C does not allow functions to be nested this way. All functions in C are at the top
lexical level.


This restriction slightly simplifies C implementations. In languages like Pascal, Ada,
Modula-2, PL/I, or Algol-60, which do allow lexically nested procedures, an activation
record will typically contain a pointer to the activation record of its enclosing function. This
pointer is termed a static link, [1] and it allows the nested procedure to access the stack frame
of the enclosing procedure, and hence the data local to the enclosing procedure. Remember
that there may be several invocations of an enclosing procedure active at once. The static
link in the nested procedure's activation record will point to the appropriate stack frame,
allowing the correct instances of local data to be addressed.


This type of access (a reference to a data item defined in a lexically enclosing scope) is
known as an uplevel reference. The static link (pointing to the stack frame of the lexically
enclosing procedure determined at compiletime) is so named because it contrasts with the
dynamic link of the frame pointer chain (pointing to the stack frame of the immediately
previous procedure invocation at runtime).


In Sun's Pascal compiler, the static link is treated as an additional hidden parameter, passed
at the end of the argument list when needed. This allows Pascal routines to have the same
activation record, and thus to use the same code generator, and to work with C routines. C
itself does not allow nested functions; therefore, it does not have uplevel references to data
in them, and thus does not need a static link in its activation records. Some people are
urging that C++ should be given the ability to have nested functions.


[1] Don't, for God's sake, confuse the static link in a procedure activation record, which permits uplevel


references to local data in a lexically enclosing procedure, with a static link of the previous chapter that
describes the obsolete method of binding a copy of all the libraries into an executable. In the previous
chapter, static meant "done at compiletime". Here, it means "referring to the lexical layout of the program".


The code example below will be used to show the activation records on the stack at various points in
execution. This is a hard concept to represent in a book, because we have to deal with the dynamic
flow of control rather than the static code that a listing shows. Admittedly, this is difficult to follow,
but as Wendy Kaminer remarked in her classic psy-chological text, I'm Dysfunctional; You're
Dysfunctional, only people who die very young learn all they really need to know in kindergarten.


1 a (int i) {

Free download pdf