Graphics Programs Reference
In-Depth Information
The heap segment is a segment of memory a programmer can directly
control. Blocks of memory in this segment can be allocated and used for
whatever the programmer might need. One notable point about the heap
segment is that it isn't of fixed size, so it can grow larger or smaller as needed.
All of the memory within the heap is managed by allocator and deallocator
algorithms, which respectively reserve a region of memory in the heap for
use and remove reservations to allow that portion of memory to be reused
for later reservations. The heap will grow and shrink depending on how
much memory is reserved for use. This means a programmer using the heap
allocation functions can reserve and free memory on the fly. The growth of
the heap moves downward toward higher memory addresses.
The stack segment also has variable size and is used as a temporary scratch
pad to store local function variables and context during function calls. This is
what GDB's backtrace command looks at. When a program calls a function,
that function will have its own set of passed variables, and the function's code
will be at a different memory location in the text (or code) segment. Since
the context and the EIP must change when a function is called, the stack is
used to remember all of the passed variables, the location the EIP should
return to after the function is finished, and all the local variables used by
that function. All of this information is stored together on the stack in what is
collectively called a stack frame . The stack contains many stack frames.
In general computer science terms, a stack is an abstract data structure
that is used frequently. It has first-in, last-out (FILO) ordering , which means the
first item that is put into a stack is the last item to come out of it. Think of it
as putting beads on a piece of string that has a knot on one end—you can't
get the first bead off until you have removed all the other beads. When an
item is placed into a stack, it's known as pushing , and when an item is removed
from a stack, it's called popping .
As the name implies, the stack segment of memory is, in fact, a stack data
structure, which contains stack frames. The ESP register is used to keep track
of the address of the end of the stack, which is constantly changing as items
are pushed into and popped off of it. Since this is very dynamic behavior, it
makes sense that the stack is also not of a fixed size. Opposite to the dynamic
growth of the heap, as the stack changes in size, it grows upward in a visual
listing of memory, toward lower memory addresses.
The FILO nature of a stack might seem odd, but since the stack is used
to store context, it's very useful. When a function is called, several things are
pushed to the stack together in a stack frame . The EBP register—sometimes
called the frame pointer (FP) or local base (LB) pointer —is used to reference local
function variables in the current stack frame. Each stack frame contains the
parameters to the function, its local variables, and two pointers that are nec-
essary to put things back the way they were: the saved frame pointer (SFP) and
the return address. The SFP is used to restore EBP to its previous value, and the
return address is used to restore EIP to the next instruction found after the
function call. This restores the functional context of the previous stack
frame.
Search WWH ::




Custom Search