3 ms·
The compiler very rarely knows that UB exists in the program. To give a concrete example: the compiler sees this: ``` while (<expr>) { if (p =
by Diggsey 6y ago
The compiler very rarely knows that UB exists in the program.
To give a concrete example: the compiler sees this:
```
while (<expr>) {
if (p == 0) { do_thing(); }
*p = 42;
}
```
In this case, the compiler wants to move `do_thing()` outside the loop because it's expensive. It also knows that it would be UB for this branch to execute if `*p = 42` executes, because that would result in UB.
Therefore the compiler can assume that if the loop runs at least once, then `p != 0`. At no point does the compiler have any information about whether the program is in fact UB.