In C++, how can undefined behaviour later in a function change what code before it does, and how do C++26 observable checkpoints limit that?
answer
- the whole execution loses its meaning
- not even earlier operations
- exists_in_table with <=
- defined prefix before a checkpoint
basics
~20 sUp to C++23, an execution that will hit undefined behaviour has no requirements at all, even for operations before it, so a compiler may reshape earlier code. C++26 protects only what precedes an observable checkpoint, such as a returned stdio write or std::observable_checkpoint().
solid answer
~50 sThe C++23 standard says that if an execution contains an undefined operation, it places no requirement on the implementation, **not even with regard to operations preceding** that operation. So when the compiler can see that a path always reaches undefined behaviour, it may treat the whole path as impossible, including the code before the faulting line: the classic `exists_in_table` loop that reads `table[4]` of a four-element array may legally become `return true;`. Real compilers apply this unevenly; Apple clang 21 at `-O2` still reads `table[4]`, and it keeps an earlier `puts` call, since a call it cannot see into might never return. C++26 introduces **observable checkpoints**: observable behaviour that happens before a checkpoint, which in turn happens before the undefined operation, must be produced. The return from a `<cstdio>` call that delivers data to the host environment is one, and `std::observable_checkpoint()` in `<utility>` makes one explicitly; anything after the last checkpoint is still unprotected.
go deeper
Remember that undefined behaviour can affect the whole run, not just the line that causes it.
Explain the exists_in_table example and why reaching undefined behaviour later makes an earlier branch removable.
Separate what the standard permits from what a given compiler does, explain why opaque calls usually survive, and describe C++26 observable checkpoints and the defined prefix.
Judge where C++26 checkpoints are worth placing, such as before risky steps in long-running services, versus removing the undefined behaviour outright.
## What "time travel" means Most programmers picture undefined behaviour as a fault at one line: everything before it ran normally, everything after it is suspect. The C++23 standard says something stronger. In its description of the abstract machine, a conforming implementation must reproduce the observable behaviour of the program's possible executions, "however, if any such execution contains an undefined operation, this document places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation)". Optimizers use this whenever they can prove that a path inevitably reaches undefined behaviour. Such a path cannot occur in a valid execution, so the branch leading to it, and anything that only happens on it, can be removed or rearranged. Nicknamed **time travel**, it is the same reasoning as the deleted overflow and null checks, applied backwards. ## The textbook example ```cpp int table[4]; bool exists_in_table(int v) { for (int i = 0; i <= 4; ++i) // reads table[4]: one past the end if (table[i] == v) return true; return false; } ``` Reasoning a compiler is allowed to follow: 1. If none of `table[0]` to `table[3]` matches, the loop reads `table[4]`, which is undefined. 2. A valid execution therefore always returns `true` from one of the first four iterations. 3. So the function can be compiled as `return true;`, and the first four comparisons, which ran **before** the bad read, never happen. This is a licence, not a prediction. Apple clang 21 at `-O2` vectorizes the first four comparisons and still performs a separate load of `table[4]`; other compilers or releases may do otherwise. The interview point is that the standard permits the transformation, so the earlier lines are not protected by coming first. ## Why I/O usually survives, and why that is not a rule A second classic is a diagnostic printed before a fault: ```cpp int payload_length(const Packet* p) { if (p == nullptr) std::puts("null packet"); // before the dereference return p->length; // undefined if p is null } ``` Under C++23 the compiler may reason that `p` cannot be null, because the following dereference would be undefined, and drop the message. Apple clang 21 keeps the `puts` call: a call into code the compiler cannot see might never return, for example by ending the process, so the later dereference is not provably reached. Replacing the call with a plain store to a global counter changes nothing for Apple clang 21 either, but C++23 would allow removing it. The safe C++23 reading is: **nothing before the undefined operation is guaranteed**, and anything you observe is the compiler's choice. Note also that `puts` into a buffered `stdout` may not deliver anything to the operating system before the crash, which matters for the C++26 rule below. ## What C++26 changes C++26 rewrote this part of the abstract machine: - Certain events are **observable checkpoints**. The draft names a call to `std::observable_checkpoint()`, declared in `<utility>`; the return from each `<cstdio>` function call that delivers data to the host environment to be written to a file; writes by a file stream buffer; and certain parts of the evaluation of contract assertions. - The **defined prefix** of an execution is the set of operations that happen before some checkpoint that itself happens before every undefined operation. - An implementation must produce the observable behaviour of the defined prefix; after an undefined operation it may add arbitrary observable behaviour. | Situation | C++23 | C++26 | |---|---|---| | output delivered to the host by a returned `<cstdio>` call, then UB | not guaranteed | must be produced | | output still sitting in a stream buffer, then UB | not guaranteed | not guaranteed: nothing was delivered | | a store to an ordinary variable, then `std::observable_checkpoint()`, then UB | not guaranteed | part of the defined prefix | | anything after the UB | not guaranteed | not guaranteed | So C++26 does not abolish time travel; it pins down what has already left the program, and gives you an explicit call to fence the rest. Neither Apple clang 21's libc++ nor the libc++ headers in the pinned LLVM sources declare `std::observable_checkpoint` yet. ## What to do in real code - Treat any line before a known undefined operation as unreliable when you debug: a "missing" log line is not proof the code before it did not run, nor proof that it did. - Fix the undefined operation; do not try to "protect" earlier code by moving logging around. - In C++26 code that must leave a trace before a risky step, flush the output so that a delivering `<cstdio>` call returns before the step, or call `std::observable_checkpoint()`. - Use sanitizers on an optimized build to find the undefined operation itself.
- Does C++26 make output written before undefined behaviour reliable?Output that was actually delivered is: the return from a `<cstdio>` call that delivers data to the host environment is an observable checkpoint, so that output belongs to the defined prefix. Data still in a stream buffer was not delivered and is unprotected, so flush before a risky step. `std::observable_checkpoint()` adds a checkpoint explicitly.
- Why do compilers rarely delete an I/O call that precedes undefined behaviour?To delete it, the compiler must prove the undefined operation is always reached after the call. A call into a library function the compiler cannot see might never return, for example by exiting, so the later operation is not provably reached and the call stays. That is a limit of the analysis, not a guarantee in the standard.
- Undefined behaviour only affects the code that runs after the faulting line.
- If a log line before the crash printed, the code before it ran correctly.
- C++26 abolished time travel, so earlier operations are always preserved.
- Compilers always turn exists_in_table with <= into return true.
Assumes C++23's rule that undefined behaviour voids even earlier operations; observable checkpoints and std::observable_checkpoint are C++26.