Heapless C++

Please log in to access your purchased courses.

C++ is not inherently less suited for high-constraint fields like embedded systems, high-frequency trading, financial systems, or game development – no matter what myths C-only programmers spread. However, they are correct in that we need to be selective and fine-tune our usage of the C++ language. The most common solutions to minimize overhead and make heap usage predictable are:

In this course we will go one step further: remove the heap altogether. By using a subset of C++, we will keep the expressiveness and optimized code generation of C++ without any heap allocation overhead.

Course Overview

In this course, we provide a hands-on approach for learning a diverse set of patterns, tools & techniques for writing C++ code that never uses the heap. We really mean never: rather than implementing a “heap lite”, which is a common workaround, we show you how to completely avoid the need for a heap-like construct. Even if your system does allow for use of the heap, this course can still benefit you by demonstrating techniques that you can apply to make your software faster, safer, and more correct.

In this course, you will learn:

  • Exactly which parts of the C++ standard library you can use without the heap
  • Specific techniques for avoiding the heap by using alternate forms of memory, such as the .text segment, statically allocated data, and the stack
  • How to implement compile-time alternatives for type information and polymorphism
  • Ways to handle errors without using exceptions
  • Static memory containers that replace the STL containers while providing equivalent interfaces and behaviors
  • And much more!

Instead of a theoretical study that would get lost in the details, this course takes a practical approach. Each technique we present will be summarized and demonstrated using example code. We use the amazing Compiler Explorer in this course, which enables you to explore the generated code yourself while reading about how it works. We will also provide you with links to libraries that can be used as references and complex examples of the techniques we discuss. In addition, we provide a number of exercises (with solutions) that you can use to master these new techniques.

At the end of this course, you will have a diverse set of patterns, tools & techniques to create C++ code without ever using the heap. You’ll be able to write deterministic & safe programs, that are faster and consume less memory.

Time Commitment

Using an average reading speed of 250 wpm, we estimate that the course will require:

  • Core Reading: 1 hour
  • Exercises: 6 hours

Who is This Course For?

The content is targeted at intermediate-level C++ developers who are looking to solve the challenge of using C++ in an environment where you cannot use the heap. This course is also beneficial for developers who want to develop a deeper understanding of memory management concepts and mechanisms available in C++.

No embedded experience is required, since this course specifically focuses on the heapless aspect.

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.

Prerequisites

This course assumes that you already know how to program in modern C++ (C++11 or later). We do not provide a general introduction to C++. Students who are new to C++ can still benefit from this course, but you are expected to self-study in order to master the material that you do not understand.

No hardware is required for this course.

Support

We’re here to help you if you get stuck! Our course is designed to be as comprehensive as possible, but also challenging. You can always ask questions or get help in the Course Support section of the forum, through private messages in the course interface, and also through private messages in the forum.

Getting Started (Klemens Morgenstern)

Introduction to Heapless C++ (Klemens Morgenstern)

We discuss different regions of memory, heap storage, and our concerns with heap-based allocation.

Lessons

What is the Heap? C++ Without the Heap

Types (Klemens Morgenstern)

We look at alternative approaches to polymorphism and run-time type information that avoid the heap.

Lessons

Polymorphism Compile-Time Type Information Compile-Time Polymorphism

Single Value Containers (Klemens Morgenstern)

Views (Klemens Morgenstern)

We discuss strategies for creating non-owning references to our data and accessing underlying data using container-like interfaces.

Lessons

Views std::string_view std::span Ranges Intrusive Containers

Error Handling (Klemens Morgenstern)

We discuss ways to handle errors without relying on exceptions or the heap.

Lessons

Error Handling Without Exceptions

Static Containers (Klemens Morgenstern)

We review static memory alternatives for containers that hold multiple items.

Lessons

Static Containers Tuples

Conclusion (Klemens Morgenstern)

Contains a summary of the course, final downloads, and other supplemental items.

Lessons

Conclusion: Heapless C++