Memory Layout for Compiled Program

Operating System
User Process Code
Machine Code
User Process Data
Dynamically Allocated Heap
Global Static Data (Const,Var)

Both dynamically allocated heap and stack expand

  • heap need not be contiguous can request more memory from the OS if needed
  • stack grows downwards

Heap is more general:

  • can allocate, read/write, and deallocate in any order
  • Garbace Collector does deallocation automatically
  • More in Lecture 14

Stack is more efficient - allocation simple increment, decrement

Top of stack pointer (SP) is often a register

If stack grows towards smaller addresses:

  • to allocate N bytes (push)

SP := SP - N

  • to deallocate N bytes (pop)

SP := SP + N