Graphics Programs Reference
In-Depth Information
After the execution finishes, the entire stack frame is popped off of the
stack, and the EIP is set to the return address so the program can continue
execution. If another function was called within the function, another stack
frame would be pushed onto the stack, and so on. As each function ends, its
stack frame is popped off of the stack so execution can be returned to the
previous function. This behavior is the reason this segment of memory is
organized in a FILO data structure.
The various segments of memory are arranged in the order they
were presented, from the lower memory addresses to the higher memory
addresses. Since most people are familiar with seeing numbered lists that
count downward, the smaller memory addresses are shown at the top.
Some texts have this reversed, which can be very confusing; so for this
book, smaller memory addresses
are always shown at the top. Most
debuggers also display memory in
this style, with the smaller memory
addresses at the top and the higher
ones at the bottom.
Since the heap and the stack
are both dynamic, they both grow
in different directions toward each
other. This minimizes wasted space,
allowing the stack to be larger if the
heap is small and vice versa.
Low addresses
Text (code) segment
Data segment
bss segment
Heap segment
The heap grows
down toward
higher memory
addresses.
The stack grows
up toward lower
memory addresses.
Stack segment
High addresses
0x271
Memory Segments in C
In C, as in other compiled languages, the compiled code goes into the text
segment, while the variables reside in the remaining segments. Exactly which
memory segment a variable will be stored in depends on how the variable is
defined. Variables that are defined outside of any functions are considered
to be global. The static keyword can also be prepended to any variable
declaration to make the variable static. If static or global variables are initial-
ized with data, they are stored in the data memory segment; otherwise, these
variables are put in the bss memory segment. Memory on the heap memory
segment must first be allocated using a memory allocation function called
malloc() . Usually, pointers are used to reference memory on the heap.
Finally, the remaining function variables are stored in the stack memory
segment. Since the stack can contain many different stack frames, stack
variables can maintain uniqueness within different functional contexts.
The memory_segments.c program will help explain these concepts in C.
memory_segments.c
#include <stdio.h>
int global_var;
Search WWH ::




Custom Search