You are on page 1of 2

Memory Layout (Virtual address space of a C process)

System High memory Stack illustrated after the call


func(72,73) called from main(),
assuming func defined by:
env func(int x, int y) {
STACK

argv
argc int a;
mfp − frame pointer (for main) int b[3];
auto variables for
main() /* no other auto variables */
auto variables for Assumes int = long = char * of
func()
stack pointer size 4 and assumes stack at high
(grows downward if func() address and descending down.
calls another function)
available for Expanded view of the stack
stack growth
Stack

Offset from current main()


frame pointer (for auto Contents
func()) variables
MEMORY
SHARED

malloc.o (lib*.so) library functions if +12 73 y


dynamically linked +8 72 x
printf.o (lib*.so) (usual case) +4 ra return address
frame pointer 0 mfp caller’s frame pointer
available for points here −4 garbage a
heap growth −8 garbage b[2]
brk point −12 garbage b[1]
−16 garbage b[0]
Heap stack pointer
(malloc arena) (top of stack)
points here

All auto variables and parameters


DATA

global variables uninitialized data (bss) are referenced via offsets from the
frame pointer.

"...%d..." initialized data The frame pointer and stack pointer


are in registers (for fast access).
malloc.o (lib*.a) library functions if
compiled code (a.out)

statically linked When funct returns, the return value


TEXT

printf.o (lib*.a) (not usual case) is stored in a register. The stack pointer
file.o is move to the y location, the code
func(72,73) ra (return address) is jumped to the return address (ra),
main.o and the frame pointer is set to mfp
crt0.o (startup routine) (the stored value of the caller’s frame
Low memory pointer). The caller moves the return
value to the right place.