Memory: stack and heap

Published

2023-08-05

There are two pools of memory that are available to you as a C++ programmer: the stack and the heap. Until now, we’ve been using the stack. This video (9:30) explains the difference between the stack and the heap, and shows how to allocate and deallocate memory on the heap.

Schematic of computer memory, indicating stack and heap

Resources

Comprehension check

  1. Ordinarily, space for variables in C++ is allocated on the ___________.
  2. Every function call gets its own _________________________ which includes parameters passed to the function, locally declared variables, and a return address.
  3. Memory leaks are caused by a failure to ______________________________ space on the heap when it is no longer needed.
  4. The amount of _______________________ memory is fixed.
  5. Data on the heap is accessed via _____________________________.

Answers: sɹǝʇuᴉod / ʞɔɐʇs / ǝʇɐɔollɐǝp / ǝɯɐɹɟ ʞɔɐʇs / dɐǝɥ

Amusement

Memory leaks

Source: Shen Comix (with modifications by some anonymous programmer)

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

All materials copyright © 2020–2023, The University of Vermont. All rights reserved.