liveBook · Manning

Memory safety

As explained in Memory Safe C++

The property that a program's memory operations do not cause errors such as out-of-bounds access or corruption.

An out-of-bounds read can have consequences far beyond the process that performs it. In 2024, a CrowdStrike Falcon update supplied 20 inputs where an interpreter expected 21; without a runtime bounds check, it read a nonexistent entry and crashed. Hospitals, airlines, and emergency services were among those disrupted. section 1.0

Memory safety means that memory operations avoid errors such as out-of-bounds access and corruption. It matters in all C++ software, but defects in safety-critical systems can cause irreversible harm. Correct results are not enough in a real-time system: they must also arrive on time. section 1.0 section 1.10

C++ offers tools for writing memory-safe code, but it also permits dangerous patterns alongside them. A memory defect can compile cleanly, so safety must be built deliberately rather than assumed from a successful build. section 1.6 section 1.7

A sensor label without a terminator

A patient-monitoring function copies a sensor label into a 32-byte, zero-initialized array, then passes it to a logger expecting a null-terminated C string. A 32-character label fills the array without overflowing, but overwrites every zero. The logger then reads past the array while looking for a terminator. A copy that fits is not necessarily a safe string operation. section 1.7

Listing 1.1 Sensor label copy without bounds check
 
void ProcessSensorReading(std::span<const std::uint8_t> raw_data) {
   constexpr std::size_t kMaxLabelLength = 32;        #A
   std::array<char, kMaxLabelLength> sensor_label{};  #B
 
   auto reading = ParseSensorData(raw_data);
 
   std::copy(reading.label.begin(),  #C
             reading.label.end(),
             sensor_label.begin());
 
   LogReading(sensor_label.data(), reading.value);  #D
}

Inspect the unchecked copy and the subsequent call that treats the array as a null-terminated string. section 1.7

A systematic approach combines safe design patterns, static analysis, dynamic analysis, and code review. It also depends on three foundations: knowledge of hazards such as buffer overflows and use-after-free; discipline to apply practices consistently; and a team culture that values safety over shortcuts. Automated checks help maintain discipline under deadline pressure. section 1.10 section 1.7

Safety standards, coding guidelines, language features, and defensive programming contribute at different levels. Standards define required outcomes; guidelines constrain permitted constructs; and verification checks that the code meets the requirements. No single feature or review replaces the whole process. section 2.3 section 1.10

How Memory Safe C++ covers it

How deep it goes
Explains a missing-terminator failure and outlines a layered safety process supported by knowledge, discipline, and culture.
Examples
Uses the CrowdStrike outage and a patient-monitoring sensor label to illustrate out-of-bounds reads.
What you can do after
Evaluate/understand memory risks and recognize why complementary practices are needed to build safer C++ software.
How it connects
Connects memory safety to real-time systems, static analysis, dynamic analysis, code review, safety standards, and coding guidelines.

Also explained in

Compare how 5 books explain memory safety →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Memory Safe C++. The section links point to the parts of the book it draws on.