Week 7 - Memory Management
Learning objectives
Upon finishing this week, you should be able to:
- Accurately describe how a program is using the stack, heap and pointers
- Use destructors and copy constructors appropriately in classes
- Describe and diagram the difference between a deep and a shallow copy
- Write simple container classes that manage dynamic memory
Suggested pacing
Day 1
- Holiday
Day 2
- Dynamic Memory (Ch22.1-22.6)
- Pointer Drawing WS
Day 3
- Setup/Experiment with Address Sanitizer
- Arrays (Ch22.8-22.9)
- Ch 22 Exercises
Day 4
- Container Classes (Ch22.10-22.15)
- Optional: Tools for managing memory
- Optional: Rule of Five
Activity Outline
Dynamic Memory
Read Ch 22.1-22.6 on dynamic memory.
Do this worksheet to develop your understanding of the mechanics of dynamic memory:
This optional video review the concepts:
Valgrind
Read 22.7.
Address Sanitizer is a great tool, but it is not available in Windows or the Mac versions of the compiler we are using. There are also some kinds or memory issues that Address Sanitizer doesn't catch. So we will set up and use a different tool verifying memory usage.
Windows users should install Windows Subsystem for Linux (WSL) and use Valgrind in WSL.
Linux users (or if you work in a codespace) can use Valgrind without any extra setup.
Mac users should install Leaks.
Find instructions for these tools here:
Do the Valgrind assignment. (Take a screenshot of using valgrind or leaks to test a program.)
Windows Users - Setup WSL
If you are on Windows you should install Windows Subsystem for Linux (WSL). Address Sanitizer is not available on Windows, but it is available in WSL. So you need to use a codespace or WSL to use Address Sanitizer.
Longer term, you will increasingly find it useful to have a Linux environment available for programming. WSL is a great way to get that without needing to set up a separate machine or dual boot.
These instructions will help you get it set up and use it to build and run code:
WSL Installation and Use Guide
Arrays
Pointers and arrays are surprisingly interchangeable in C/C++.
Read 22.7-22.8.
Then do the Ch 22 Exercises.
This optional video reviews some of the concepts:
Container Classes
Read 22.10-22.15 on using classes to manage memory.
This optional video explains the core ideas:
And this one explains the copy constructor and assignment operator:
Tools for Managing Memory
Optional
Garbage Collection and Smart Pointers
You may be starting to realize that managing memory on our own is difficult and error-prone. Fortunately, it is not something that we always have to do. Many programming languages provide tools to help remove some of the burdens.
This video introduces two of these tools - garbage collection and smart pointers:
Rust
Rust is a programming language that uses ownership of memory as one of its core design principles. In Rust, the goal is to never have memory that is "shared". All memory is owned by exactly one reference. We may let the memory be borrowed temporarily by something like a function, but the original reference cannot use the memory while it is borrowed and the memory may only be borrowed by one reference at a time.
This video introduces this way of thinking about memory:
Rule of Five
Optional
In modern C++ there are two other operators related to the rule of three: the move constructor and move assignment operator. They are used to write efficient code, but are not necessary. Failing to use them does not cause the same kind of errors that forgetting to implement a destructor or custom assignment operator does. This level of efficiency is the kind of thing a professional C++ developer should care about - but for now we have better uses for your brain cells.
If you want to learn more about them check the learncpp.com coverage of move constructor and move assignment operator.