What is difference between stack and heap memory allocation?

  • stack is memory allocated for temporary variables used by subroutines
  • heap is memory allocated for long term data structures (e.g. linked lists, trees) that are likely to change size

Both are forms of dynamically allocated memory (i.e. allocated/deallocated at runtime as needed), but the allocation/deallocation method and their place in physical/virtual memory are different

Statically allocated memory (i.e. allocated at compile/link time) is used for variables and data structures that must exist as long as the program is running and cannot change in size while the program is running.