3 ms·
To be specific, only certain types of overflow are undefined behaviour. Signed integers and pointers have undefined behaviour on overflow. Unsigned integers on
by frederikvs 9y ago
To be specific, only certain types of overflow are undefined behaviour. Signed integers and pointers have undefined behaviour on overflow. Unsigned integers on the other hand are defined to wrap around to 0.
For those interested, good places to start learning more about this are John Regehr's blog [0], or the llvm blog [1].
[0] http://blog.regehr.org/archives/213 http://blog.regehr.org/archives/213
[1] http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- cperciva 9y agoTechnically unsigned integer wrapping isn't considered to be "overflow".
- cperciva 9y agoReplying to myself because it's too late to edit the comment: I'm not sure why people are voting that down. Section 6.2.5 paragraph 9 of the C99 standard states that: A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.