Signed vs Unsigned Overflow
Discussions center on the differences in overflow behavior between signed and unsigned integers in C/C++, where signed overflow is undefined behavior allowing aggressive compiler optimizations, while unsigned overflow wraps around in a well-defined manner.
Activity Over Time
Top Contributors
Keywords
Sample Comments
Umm, signed integers are UB on overflow; unsigned is always fine.
unsigned overflows are well defined, but signed ones are not
Signed overflow is undefined behavior.
It relies on undefined behavior since signed integer overflow is undefined.
You're not asking the right question. Most language don't make a distinction between signed and unsigned integer to begin with. And the error being showcased has nothing to do with weird conversions or over/underflows either.
Tweet said unsigned ints, so donβt think this would work. Even with signed I think this can overflow.
Yes, as unsigned overflow is fine by default. AFAIK the issue was originally that there were still machines that used ones complement for describing negative integers instead of the now customary twos complement.
Probably. Signed integer overflow is undefined behaviour in C and C++.
The compiler cannot assume that much, because the argument is a signed integer (negative integers will not overflow and do have well-defined behaviour).
The compiler can't warn about something like this? I guess unsigned integer underflow can be the intended behavior often.