|
|
|
|
|
by bregma
1689 days ago
|
|
> You need not imagine an alternate version of C, such a version of C is provided by any decent C compiler. Classic misunderstanding of undefined behaviour. In this case it's still undefined behaviour, but the vendor has said "this is what my compiler will do with this particular undefined behaviour under these circumstances". Vendors are allowed to do anything they want when code containing undefined behaviour is submitted to their compiler, including doing something you might consider sane. |
|
Nevertheless, in such cases it is the responsibility of the programmer to either write programs in which it is guaranteed that events for which the program behavior is undefined will never happen or to choose compiler options to handle such events.
If the programmers do their job right, then the compiler is free to optimize like when the undefined cases do not exist.
Unlike the case with integer overflow, there are cases where the programming language standards correctly leave some behavior as undefined, e.g. the order of evaluation for function arguments. For that kind of undefined behavior the programmer must take care to write only programs whose effects do not depend on it.
Data dependent events like integer overflow cannot always be avoided, so compilers must have options to generate exceptions when they happen.