



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Suppose you’ve written a program which defines and uses an array of a fixed size and, of course, of a given type. Say, for instance, it’s an array of char ’s of size 10.^1
The interesting property of an array is that you can directly set and retrieve any of its elements in a single operation; if the array is named A, then the assignment A[7] = ‘P ’ sets the 8-th element to the character ‘P ’.^2 However, if you needed to search the array for a given character, you’d have to check every element until you find the one you’re looking for, unless the array is sorted, in which case you can use a faster search algorithm called binary search. In general, however, you’re stuck with a search that may take as many steps as the size of the array.
This informal analysis shows that we can characterize an array by the kind and speed of the operations we can perform on it: access to any element (an operation known as random access) is fast, but searching an array is comparatively expensive, potentially taking as many steps as the size of the array. Element deletion is equally expensive, since we may have to relocate a large portion of the array elements after the deletion.
An array is an example of a data structure: an arrangement of memory cells for which a very specific set of operations is defined, some of which may be fast and others which may (^1) Since the array’s type and size are both known at compile time, the compiler is able to generate code to allocate enough memory for the entire array, namely, 10 bytes in our example (recall that a char takes up a single byte). (^2) Not the 7-th element, because array indices start at 0.
be slow.
Another example of a data structure is a stack, whose behavior is what one would expect from its name. Just as you add or take a tray only from the top of the stack of trays in the cafeteria, a stack data structure lets you insert a new element onto its top or retrieve (from the top) the last element inserted. In the data structure jargon, the insertion operation is called a push and the retrieval operation is called a pop. The important thing to remember about a stack is that you can only insert or retrieve at the top of the stack; all other elements are not accessible until they are exposed at the top by successive pops.
From the definition above, it’s clear that pushing and popping are very fast operations. But, like an array, searching the stack requires you to pop every element until you find the one you’re looking for. It might seem, therefore, that a stack is very similar in behavior to an array. Not so! An array allows fast random access, whereas random access in a stack is as slow as searching it.
How does one actually build a stack? There are different ways to implement a stack, each with its own advantages and disadvantages. I’ll present here the simplest one: an array implementation.
Suppose, then, that you have a fixed-size array stack of, say, characters. In order to build a stack data structure from this array, we also need to define an integer, top. For simplicity, suppose the array has size 5. Initially, top will have a value of −1, indicating that the stack is empty. As we push elements into the stack or pop elements from it, top will change accordingly, either increasing or decreasing. When top equals (size of the array minus 1), we’ll know then that the stack is full and no more pushes should be allowed. Similarly, if we perform enough pops, top will reach −1 again, indicating that the stack is empty and we should not allow any more pops from it.
The actual operations push and pop can be defined as follows:^3
void push ( char c ) { top++; s t a c k [ top ] = c ; }
char pop ( ) { char c = s t a c k [ top ] ; top −−; return c ; }
(^3) Actually, these functions should test for stack-empty and stack-full conditions but, for simplicity, we’ll ignore those tests here.
All of these areas are allocated by the operating system immediately prior to start- ing execution. Graphically, at some point during the program’s execution, these different memory regions are typically laid out as in the figure below:
Program Code
Constant Data
Heap Data Area
Global Data Area
Free Area
Stack Data Area
Lower Addresses
Higher Addresses
Read-Only Area
Activation Frame 1
fp1 [not used]
Activation Frame 2
fp2 [= address(fp1)]
Activation Frame 3
fp3 [= address(fp2)]
Registers
pc
Current fp [= address(fp3)]
sp
Note that the sizes of the global data, constant data, and program code areas are all known at compile time, so the compiler knows how much memory to allocate for those areas.
The stack and heap areas, due to their dynamic nature, are of variable size so the best the compiler can do is allocate a reasonable chunk of memory for both areas combined. It’s the programmer’s responsibility to manage the program’s use of heap and stack memory to prevent an out-of-memory condition. Note also how the stack is set-up: it grows towards lower addresses.
A separate area of memory stores register data, the program counter (pc), the stack pointer (sp), and the current frame pointer (fp). In this simple handout, we will not talk much about registers. The program counter is the address of the next program instruction to execute, and the stack pointer is the address of the current top of the stack.
Every time the program execution encounters a function call, a structure called an activation frame is appended to the stack area. This structure is filled partially by the block calling the function and partially by the function being called. The figure below illustrates a typical activation frame.
Arg 2 Arg 1 Caller's Return Address fp [= address(previous fp)]
Local Variables
Saved Registers
Saved by the caller
Saved by the callee
Activation Frame
Lower Addresses
Higher Addresses
sp
The calling block pushes into the stack first the arguments to the function being called (typically in reverse order), and then a return address, that is, the address of the next instruction in the program’s code after the instruction which prompted the function call (this is calculated from the program counter pc). This is done so that, when the function finishes executing and returns, the processor knows which instruction to execute next. The calling block also pushes into the stack the address in memory of the previous activation frame pointer. This is necessary because a function may call another function which may call yet another function and so on, and there must be a way to return from all these nested calls. Each of these calls is associated with its own activation frame, and the frames are linked in a chain, each pointing to its predecessor.