@rkenmi - Stack Memory vs. Heap Memory

Stack Memory vs. Heap Memory


Stack Memory vs. Heap Memory


Back to Top

Updated on August 2, 2020

Stack Memory

The stack memory is generally:

  • Faster, as it uses CPU cache
  • Fixed in size
    • It is specifically the range of memory that is accessible via the Stack register of the CPU
  • No control for allocation and de-allocation of memory
  • Includes function calls and variables for function arguments, primitive values (int, char, etc.)
  • Includes references (a.k.a pointers)
  • Memory is allocated in contiguous blocks = faster lookup
  • Can result in the dreaded stack overflow

Heap Memory

  • It uses RAM, which is slower than CPU Cache
  • Dynamic in size and in general, much much larger than the stack
    • Allocated memory can be resized at runtime
  • Control for allocation and de-allocation of memory
    • malloc and calloc are examples of manually allocating memory in the heap.
  • Includes objects that references (or pointers) point to
  • Includes arrays
  • Memory is allocated in non-contiguous blocks, so cache misses are frequent.
  • Can result in heap overflow

Article Tags:
Computer Sciencememory addressmemorystackheap