Why does safety-critical C++ firmware forbid heap allocation after start-up, and which standard C++ features allocate without saying so?
answer
- fragmentation and unbounded timing
- failure at an unpredictable moment
- containers that grow
- type erasure and shared ownership
- allocate once, then lock
basics
~10 sHeap allocation can fail or fragment at an unpredictable moment and takes unbounded time, which a pacemaker cannot tolerate. Growing containers, std::string, std::function, std::make_shared and node-based containers can all allocate implicitly.
solid answer
~50 sA device such as a pacemaker must prove that it never runs out of memory and that every deadline is met. A general-purpose heap defeats both: fragmentation can make an allocation fail after weeks of uptime, and allocation time depends on the heap's history. The usual rule is therefore **allocate during start-up, then never again**: objects live in static storage, on a bounded stack, or in fixed-capacity containers sized at compile time. In C++ the danger is allocation you did not write: `std::vector` and `std::string` growth, `std::function` holding a callable too large for its internal buffer (the buffer size is the implementation's choice), `std::make_shared` and `std::shared_ptr`, node-based containers such as `std::map` and `std::list`, and, on common ABIs, the exception object of a `throw`. Teams enforce the rule by replacing the global `operator new` with one that halts once start-up is over.
code
cpp · 32 lines#include <cstdio>
#include <cstdlib>
#include <new>
#include <string>
#include <vector>
static bool heap_locked = false;
static int allocations = 0;
void* operator new(std::size_t n) { // replaces the global allocation function
if (heap_locked) {
std::puts("heap used after start-up: halting");
std::abort(); // on the device: safe state, then reset
}
++allocations;
if (void* p = std::malloc(n)) return p;
std::abort(); // out of memory during start-up
}
void operator delete(void* p) noexcept { std::free(p); }
void operator delete(void* p, std::size_t) noexcept { std::free(p); }
std::vector<int> event_log; // sized once at start-up
int main() {
event_log.reserve(64); // allowed: still starting up
heap_locked = true; // from here on, no heap
std::printf("start-up allocations: %d\n", allocations);
for (int i = 0; i < 64; ++i) event_log.push_back(i); // fits: no allocation
std::printf("logged %zu events\n", event_log.size());
std::string note = "lead impedance out of range on ventricular channel";
std::printf("%s\n", note.c_str()); // not reached
}go deeper
Explain fragmentation and unpredictable timing, and name the standard types that allocate behind the scenes.
Describe where objects live instead and how reserve during start-up keeps later push_back calls allocation-free.
Enforce the rule with a replaced operator new that traps after start-up and a test that runs the real control loop under it.
Decide which allocations start-up may make, how stack bounds are proven, and what evidence the safety case needs.
## What the heap costs a safety device Think of a pacemaker's firmware: it runs for years, it must deliver a pacing pulse within a fixed window, and a reset at the wrong moment is a clinical event. A general-purpose heap is a poor fit for three reasons: - **Fragmentation.** After many allocations and frees of different sizes, free memory can be split into pieces too small for a request even though the total is enough. That failure appears after days or weeks, never in a short test. - **Unbounded timing.** How long `malloc` or `operator new` takes depends on the state of the free lists, so the worst case is hard to state and harder to prove. - **An awkward failure mode.** In standard C++ a failed `new` throws `std::bad_alloc`; with exceptions disabled the program usually ends. Neither is acceptable in the middle of a therapy cycle. Safety standards and coding guidelines for such devices therefore restrict dynamic allocation, and many teams adopt the strict form: memory may be allocated during initialisation, and after that the heap is closed. ## Where objects live instead | Storage | How | Bound known at | |---|---|---| | Static storage duration | namespace-scope or `static` objects, `constinit` where possible | link time (the map file) | | Automatic storage (the stack) | locals in functions with bounded call depth | analysis of the call graph | | Fixed-capacity containers | `std::array`, a hand-written fixed vector, `std::inplace_vector` in C++26 | compile time | | Objects created later in reserved space | placement new into an aligned static buffer | compile time | Recursion and variable-length arrays (not standard C++, but accepted by some compilers as an extension) are usually banned for the same reason: they make the stack's bound unknowable. ## Standard features that allocate without saying so The C++ trap is that allocation hides behind ordinary-looking code: 1. **Growing containers.** `std::vector::push_back`, `insert` and `resize`, and every `std::string` that outgrows its small-string buffer (whose size is the library's choice). 2. **Node-based containers.** Every insertion into `std::list`, `std::map`, `std::set` or `std::unordered_map` allocates a node. 3. **Type erasure.** `std::function` stores a callable that does not fit its internal buffer on the heap; `std::any` does the same. 4. **Shared ownership.** `std::make_shared` and `std::shared_ptr` allocate a control block. 5. **Exceptions.** On the common Itanium ABI the runtime allocates the exception object when you `throw`; libstdc++ tries `malloc` first and falls back to an emergency pool. 6. **Threads and futures.** `std::thread`, `std::async` and `std::promise` allocate shared state. 7. **Text and locales.** `std::to_string`, `std::format` and iostreams can allocate. ## Allocate once, then lock Some objects genuinely need sizes known only at start-up, such as buffers sized from a configuration block. The pattern is to let start-up allocate, then close the heap: - Reserve everything during initialisation (`reserve`, one-time construction). - Set a flag that marks the end of start-up. - Replace the global `operator new`, which C++ allows the program to do, with a version that halts the device in a safe state if it is called after the flag is set. The example below shows the idea on a host. A test suite that runs the full therapy loop under this replaced `operator new` turns any hidden allocation into a failed test instead of a field incident. ## C++ start-up rules that move allocation around The language's own initialization rules decide *when* an allocation happens, and that can put it on the wrong side of the lock: - **Namespace-scope objects** with dynamic initialization run their constructors before `main`, so a global `std::vector<int> v{1, 2, 3};` allocates during start-up, which is allowed. - **Function-local statics** are initialized the first time control passes through their declaration. A `static std::vector<Alert> pending(16);` inside a function first called by the therapy loop allocates long after start-up, and the trap fires in the field rather than in a bench test that never reached that path. - **Thread-safe local static initialization** is implemented with a guard; on some runtimes the guard path itself uses a lock, which matters if the first call can come from an interrupt handler. The fix is the same each time: construct such objects explicitly during initialisation, or make them `constinit` or `constexpr` so no code runs at all. ## What remains acceptable `std::array`, `std::span`, `std::string_view`, `std::optional`, `std::variant`, `std::bitset`, algorithms on existing ranges and `constexpr` tables never allocate. They are the everyday toolkit of firmware without a heap.
- Does std::vector::push_back allocate after reserve(64) in C++?Not while the size stays within `capacity()`: the standard says no reallocation takes place after `reserve()` until an insertion would make the size greater than `capacity()`, which is at least 64 here. The example therefore logs exactly 64 events after the heap is locked; pushing past the capacity would reallocate.
- Why can a throw allocate memory?The exception object must outlive the stack frames being unwound, so it cannot live on the stack of the throwing function. On the Itanium C++ ABI the runtime allocates it; libstdc++ uses `malloc` and falls back to an emergency pool if that fails. Firmware that bans the heap usually disables exceptions too.
- The heap is fine as long as total usage stays below the RAM size.
- std::function never allocates because it is a standard type.
- std::string never allocates for short text on every library.
- Banning new in your own code removes every heap allocation.
- Recursion is safe if the stack is large enough on the bench.