Is there some meaningful statistical data to justify keeping signed integer arithmetic overflow undefined?

前端 未结 4 1800
野性不改
野性不改 2020-12-29 03:20

The C Standard explicitly specifies signed integer overflow as having undefined behavior. Yet most CPUs implement signed arithmetics with defined semantics

4条回答
  •  旧时难觅i
    2020-12-29 03:52

    Not quite an example of optimization, but one useful consequence of undefined behaviour is -ftrapv command line switch of GCC/clang. It inserts code which crashes your program on integer overflow.

    It won't work on unsigned integers, in accordance with the idea that unsigned overflow is intentional.

    The Standard's wording on signed integer overflow ensures that people won't write overflowing code on purpose, so ftrapv is a useful tool to discover unintentional overflow.

提交回复
热议问题