Information Technology Reference
In-Depth Information
The Stack and the Heap
While a program is running, its data must be stored in memory. How much memory is required
for an item, and where and how it is stored, depends on its type.
A running program uses two regions of memory to store data: the
stack
and the
heap
.
The Stack
The system takes care of all stack manipulation. You, as the programmer, don't need to do
anything with it explicitly. But understanding its basic functions will give you a better under-
standing of what your program is doing when it is running, and allow you to better understand
the C# documentation and literature.
The stack is an array of memory that acts as a last-in, first-out (LIFO) data structure. It
stores several types of data:
The values of certain types of variables
The program's current execution environment
Parameters passed to methods
Facts About Stacks
The following are additional facts you should know about stacks:
Data can only be added to and deleted from the top of the stack.
Placing a data item at the top of the stack is called
pushing
the item onto
the stack.
Deleting an item from the top of the stack is called
popping
the item from the stack.
Figure 3-6 illustrates the functions and terminology of the stack.
Figure 3-6.
Pushing and popping on the system stack