What do C++26's erroneous behaviour and hardened standard library change about reading uninitialized locals and out-of-bounds indexing?
answer
- well-defined but wrong
- implementation-chosen value, may be diagnosed
- [[indeterminate]] opts out
- hardened precondition, terminating semantic
- heap reads stay undefined
basics
~20 sIn C++26, reading an uninitialized local is erroneous behaviour: well-defined but wrong, using an implementation-chosen value the tool may diagnose. A hardened library checks preconditions such as vector::operator[]'s index and terminates through a contract violation instead of undefined behaviour.
solid answer
~40 sC++26 adds a new category, **erroneous behaviour**: well-defined behaviour that is always a bug and that implementations are recommended to diagnose. The bytes of an automatic variable without an initializer now hold an **erroneous value** chosen by the implementation, so `int x; return x + 1;` no longer licenses the optimizer to assume anything; it may also be diagnosed or terminated. `[[indeterminate]]` opts a variable back into the old indeterminate state, and objects from `new` stay indeterminate. Separately, a **hardened implementation** checks **hardened preconditions** — `vector::operator[]`'s index, `front()` on an empty container, dereferencing an empty `optional` — and a violation becomes a contract violation evaluated with a terminating semantic. Whether an implementation is hardened is implementation-defined; a non-hardened one keeps undefined behaviour.
code
cpp · 11 lines#include <cstdio>
#include <vector>
int main() {
std::vector<int> v{1, 2, 3};
std::size_t i = v.size(); // one past the end
std::printf("reading v[%zu]\n", i);
std::fflush(stdout);
int x = v[i]; // precondition i < size() violated
std::printf("got %d\n", x);
}go deeper
Know that C++26 makes reading an uninitialized local a defined but erroneous read, and that you should still initialize every variable.
Explain erroneous versus indeterminate values, [[indeterminate]], why heap memory is excluded, and what a hardened precondition does on violation.
Discuss rolling out library hardening in production, its measured cost, the vendor modes available now, and which classes of UB C++26 leaves untouched.
Set the safety baseline: which hardening modes ship in release builds, which uninitialized-memory opt-outs are allowed, and how the policy changes as compilers adopt C++26.
## From undefined to erroneous Before C++26, reading an uninitialized local variable was **undefined behaviour**: the program had no meaning from that point, and optimizers could and did assume it never happened. C++26 introduces **erroneous behaviour**, defined as well-defined behaviour that the implementation is recommended to diagnose. Erroneous behaviour is always a consequence of incorrect code, but the program still has a meaning. The rule, in the standard's section on indeterminate and erroneous values: - When storage for an **automatic** variable is obtained, its bytes have **erroneous values**, each chosen by the implementation independently of the program's state. - Reading such a value is erroneous behaviour; the implementation may issue a diagnostic and may terminate the program. - Objects with **dynamic** storage duration — from `new` — and variables marked **`[[indeterminate]]`** still start with **indeterminate** values, and reading those remains undefined behaviour. - If an erroneous value is not valid for its type, as with a `bool` whose bit pattern is neither true nor false, the read is still undefined behaviour. ```cpp // C++26 int f() { int counter; // erroneous value return counter + 1; // erroneous behaviour: defined result, diagnosable } int g() { [[indeterminate]] int scratch; // explicitly indeterminate return scratch; // undefined behaviour, as before C++26 } ``` ## What compilers do with it Erroneous behaviour builds on experience with compiler flags such as GCC and Clang's `-ftrivial-auto-var-init=zero` or `=pattern`, which hardening builds already used to initialize locals. GCC 16 goes further: in C++26 mode, when that flag is not given, it zero-initializes automatic variables by default, while still warning about the uninitialized read. The value is the implementation's choice, so code must not rely on getting zero; the point is that the read can no longer be exploited by the optimizer or leak stale stack data. ## Why not simply define the value as zero? Defining every uninitialized local as zero would have turned a bug into a feature: code could then rely on it, and compilers and sanitizers could no longer report the read as a mistake. Erroneous behaviour keeps both properties. The program has a defined meaning, so the optimizer cannot remove checks or leak stale stack contents, and the read is still, by definition, the consequence of incorrect code, so tools are encouraged to diagnose it — the standard even permits false positives when a tool cannot prove the path is reachable. That is why the advice remains "initialize your variables", not "rely on C++26". ## The hardened standard library C++26 marks some library preconditions as **hardened preconditions**. Examples in the final draft include: - `operator[]` on sequence containers, `std::span` and `std::string_view`, which require the index to be in range; - `front()` and `back()` on containers, which require them to be non-empty; - `operator*` and `operator->` on `std::optional`, which require a value. Whether an implementation is a **hardened implementation** is implementation-defined. In a hardened implementation, each hardened precondition is checked as a contract assertion before any other observable effect, and evaluated with a **terminating semantic**: a violation ends the program instead of corrupting memory. In a non-hardened implementation, violating it is undefined behaviour exactly as before. Vendors offered this before C++26 under their own names. libc++ provides `_LIBCPP_HARDENING_MODE` with the values `_LIBCPP_HARDENING_MODE_NONE`, `_FAST`, `_EXTENSIVE` and `_DEBUG`, and documents that enabling it does not change the ABI. ## What does not change | Still undefined behaviour in C++26 | Why | |---|---| | signed integer overflow | not reclassified | | indexing a raw array out of bounds | no library precondition to harden | | use after `delete`, dangling references | lifetime errors are not covered | | data races | unchanged | | reading uninitialized heap memory | dynamic storage stays indeterminate | ## Practical advice 1. Keep **initializing variables**; erroneous behaviour reduces the damage of a bug, it does not make the code correct. 2. Turn on a **library hardening mode** in production builds where the cost is acceptable; measure it rather than assuming. 3. Use **`[[indeterminate]]`** only where profiling shows that initializing a large buffer costs too much, and document why. 4. Do not treat GCC 16's zero-initialization as a language guarantee; another implementation may pick a different value.
- Does erroneous behaviour mean the uninitialized variable reads as zero?No. The standard says each byte has an erroneous value determined by the implementation, independently of the program's state. GCC 16 happens to choose zero in C++26 mode, but another implementation may pick a pattern, and any implementation may diagnose the read or terminate. Code that relies on a particular value is still wrong; it is just no longer undefined.
- Why would a team mark a variable [[indeterminate]]?To avoid the cost of writing an erroneous value into a large automatic buffer that is filled before it is read — a stack array handed to a read call, for example. It restores the pre-C++26 rule for that variable, so a read before writing is undefined behaviour again. Use it sparingly, where profiling shows a real cost.
- In C++26 every uninitialized local is guaranteed to read as zero.
- Erroneous behaviour also covers uninitialized memory obtained from new.
- C++26 makes every out-of-bounds access throw std::out_of_range.
- Every C++26 implementation must be a hardened implementation.
- Erroneous behaviour lets the optimizer assume the read never happens.
Assumes the C++26 final draft N5050 (WG21 finished, ISO publication pending); C++23 is the latest published standard, so support varies by toolchain.