1 of 6

Section 6

Heaps vs. Stack

2 of 6

Let’s say we are using C. Would you use the stack or the heap, and why?

  1. Loop counter
  2. A small struct
  3. A very large struct

3 of 6

Stack

Heap

  • Small, short-living values
  • Not going to change size later
  • Statically-sized allocations: size known at compile time
  • Large, long-living values
  • When memory allocation might be changed in future
  • Variable-sized memory: size unknown at compile time

4 of 6

If you were writing a compiler, would you use the stack or the heap, and why?

  1. Array
  2. Hashtable
  3. (Bonus!) Floats

5 of 6

What if we want 64 bit tagged floats or ints?

6 of 6

Pick any kind of data from any programming language. Look up whether it’s stored on the stack or the heap and why!

Example: Java objects are stored on the (stack/heap) because …