3 ms·
> Signed overflow was not intended to be undefined--it was unspecified This seems wrong to me. The Standard very much calls signed overflow undefined behavior
by aw1621107 3y ago
> Signed overflow was not intended to be undefined--it was unspecified
This seems wrong to me. The Standard very much calls signed overflow undefined behavior (Section 3.3):
> If an exception occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not representable), the behavior is undefined.
In addition, overflow is explicitly listed as undefined behavior in at least two other places. For example, in section 1.6, Definitions of Terms:
> An example of undefined behavior is the behavior on integer overflow.
And in the list of undefined behavior (A.6.2):
> An arithmetic operation is invalid (such as division or modulus by 0) or produces a result that cannot be represented in the space provided (such as overflow or underflow) (3.3).
If signed overflow was not intended to be undefined, then it seems like a rather large oversight to say it's undefined behavior and to give it as an example of such.
> because it has a perfectly acceptable specification that differs between processors, and it was expected that the compiler would define it.
What you're describing is implementation-defined behavior in the C standard. If it was expected that implementations would define the behavior, why didn't the committee just... use that definition, instead of saying signed overflow is UB but actually meaning it's implementation-defined behavior?
In addition, unspecified behavior is a third category of behavior distinct from undefined and implementation-defined behavior. The Standard imposes no requirements on unspecified behavior in a correct program, and does not require that an implementation document what it does in such a situation.
[0]: https://port70.net/~nsz/c/c89/c89-draft.html https://port70.net/~nsz/c/c89/c89-draft.html