3 ms·
Is the undefined behavior applicable at runtime or at compile time ? i.e. Is it undefined behavior if there are code paths that might access an array out of bo
by noselasd 12y ago
Is the undefined behavior applicable at runtime or at compile time ?
i.e. Is it undefined behavior if there are code paths that might access an array out of bounds, but at runtime it actually never would ?
- PeterisP 12y agoThat's the exact thing that the compiler is doing - if there are code paths that might access an array out of bounds, then the compiler is assuming that in runtime it actually never would happen. With this assumption you might even deduce that int f(int x) { if (x != 42) undefined_behavior; return x; } is the equivalent of int f(int x) { return 42; } since (according to the assumption) in runtime it would always be called in a way that doesn't reach the undefined behavior, i.e., with x=42. And, of course, the many similar assumptions about array boundaries, pointer nullabilities, numbers not reaching overflow, etc.