3 ms·
The optimization opportunities do indeed come from the first option, "ignoring the situation completely with unpredictable results". That is: the compiler assum
by cesarb 3y ago
The optimization opportunities do indeed come from the first option, "ignoring the situation completely with unpredictable results". That is: the compiler assumes that the undefined behavior will not happen, and optimizes based on it. They do not "actively seek out the situation", it's in fact the opposite, they assume that the situation simply won't happen. And "deleting whole sections of code" is just the normal "unreachable code" optimization: code which cannot be reached on any feasible execution path will be deleted.
- deleted 3y ago[deleted]
- torstenvl 3y agoThat is an obvious misreading. Assume you have an unsigned char array r[] declared with 100 elements and the compiler encounters the following line of code: r[200] = 0xff; "ignoring the situation completely with unpredictable results" means translating that to the appropriate object code without bounds checking and no guarantees are made about whether that's a segfault or what your environment does. "assum[ing] that the situation simply won't happen" means eliding the code entirely. The first is required by the standard. The second is prohibited by the standard. There is no section of any version of the standard that permits compilers to assume that anything which might be UB or predicated on UB is dead code. Compilers must either ignore the fact that it's UB, or behave in a documented implementation-defined manner, or stop translation.