YouTube

Session 16 of Software Lessons, “Programming in C++: Memory, Stack, Heap, Syntax,” runs about 15 minutes.

What this session covers

  • Memory management
  • Stack versus heap
  • Basic syntax and a "Hello World" example

Key ideas

  • Stack memory holds local variables and is released automatically when a function returns.
  • Heap memory is allocated on request and stays until the program frees it, which makes it flexible but the programmer is responsible for releasing it.
  • Stack allocation is fast and has a fixed lifetime, while heap allocation is slower and lasts as long as you need it.
  • In C++ you manage memory yourself, so forgetting to release heap memory causes a leak.
  • A C++ program starts at the main function, and a Hello World program prints text using the standard library.