Managing Memory [2021 class archive]

Managing Memory is a two-day training course with programming examples taught by Patrice Roy. It is offered online from 09:00 to 17:00 MDT on Saturday, October 23rd and and Sunday, October 24th, 2021 (immediately prior to the conference).

Course Description

When reading articles written by people advocating other programming languages, one can read quite a lot of criticism about memory management in C++. In some cases, this criticism is reasonable, but quite often it is due to a misunderstanding of the mechanisms and trade-offs of that language.

This course aims to address this misunderstanding, and bring participants a more thorough understanding of memory management in C++, along with an understanding of modern C++ rules, tools and techniques. Thus, we will discuss beliefs held by some that lead to a misunderstanding of what C++ means with respect to memory management, including beliefs held by C++ programmers themselves with respect to what is actually legal C++ and what is broken or non-portable. We will do so using standard C++, avoiding compiler-specific extensions.

C++ is about control, and memory management is one area where C++ gives users a lot of control. This course will lead participants through a series of hands-on exercises where they will write a leak detector, write their own smart pointers, write their own containers and write their own allocators, in order to get a deeper understanding of how the various pieces of the memory management ecosystem fit together.

This course will concentrate on standard C++ and avoid most compiler-specific and operating system-specific techniques and tools. This is deliberate as this course aims to provide an understanding of how to handle memory in C++, and using vendor-specific extensions might distract us from this objective. It will also occasionally address concurrency issues, and will use templates quite frequently. Familiarity with concurrent programming, while not necessary, will help participants benefit more from the experience, thus we recommend familiarity with templates.

Since the class will involve problem-solving exercises, participants must bring their laptops (or equivalent development tool) and at least one recent C++ compiler of their choice; compilers that support at least C++ 17 are obviously preferable given the subject matter.

The format for this course will be a combination of lecture-style presentation of features and tricks and exercise solving. We will typically start with a concrete problem to solve and a minimal objective to achieve, and then work towards reaching that objective for some short time. A presentation of potential solutions will follow, accompanied by a discussion where participants’ input is encouraged.


The target audience is intermediate-level developers who:

  • Are familiar with modern C++, meaning C++11 and more in this case, without necessarily being experts
  • Seek to develop a deeper understanding of memory management concepts and mechanisms made available to them in C++
  • Want the programs they write to be faster, safer and more correct

Junior developers can benefit from this course if they are willing to explore this part of the C++ language. Developers coming to C++ from another language might find the course surprisingly instructive, as they might gain a better understanding of the workings of other languages too. More advanced developers might also enjoy this course if looking for a greater familiarity with modern C++ and its usefulness in their daily tasks.

Course Topics

Topics covered in this course include:

  • Fundamentals
    • Representation of memory in C++
    • Objects, pointers, references
      • Understanding lifetime
      • Optimizations and undefined behavior
      • Size, alignment, padding
    • Arrays
    • Pointer arithmetic (including misconceptions)
    • cv-qualifications
    • Casts
  • RAII-related techniques
    • Using destructors
    • Some pitfalls
    • lock_guard / scope_guard
    • Using standard smart pointers
      • Handling objects
      • Handling arrays
      • Custom deleters
    • Writing smart pointers
      • Ownership semantics (reference, owning, shared, duplicating)
      • Writing a small unique_ptr
      • Writing a policy-based duplicating pointer
  • Overloading memory allocation mechanisms
    • Overloading new / delete / new[] / delete[]
      • Distinguishing allocation and construction
      • Distinguishing destruction and deallocation
    • Writing a leak detector
    • Placement new
    • Arena-based memory management
    • Managing atypical memory (shared memory, persistent memory)
    • Over-aligned types
    • Sized deletion
  • Writing a generic container
    • With manual memory management
      • Exception-safety
      • Low-level standard facilities for memory management
      • Why std::launder exists
    • With a smart pointer
    • With allocator support
      • What allocators are
      • Use cases for allocators
      • Nested allocators
      • Polymorphic allocators
      • Managing allocator lifetime
  • Deferred reclamation
    • Writing a garbage-collecting allocator
  • Contemporary issues
    • Trivial relocatability
    • “Blessing” memory
    • bit_cast

Register Here

Course Instructor

Patrice Roy

Patrice Roy has been playing with C++ either professionally, for pleasure or (most of the time) both for almost 30 years. After a few years doing R&D and working on military flight simulators, he has moved on to academics and has been teaching computer science since 1998. Since 2005, he has been involved more specifically in helping graduate students and professionals from the fields of real-time systems and game programming develop the skills they need to face today’s challenges.

Patrice been a participating member in the ISO C++ Standards Committee since late 2014 and has been involved with the ISO Programming Language Vulnerabilities working group since late 2015. He has five kids, and his wife ensures their house is home to a continuously changing numbers of cats, dogs and other animals.