3 ms·
To be clear, signed integer overflow is undefined. Unsigned integer overflow is well defined. This is why some C programmers dictate that all code must use sig
by junon 5y ago
To be clear, signed integer overflow is undefined. Unsigned integer overflow is well defined.
This is why some C programmers dictate that all code must use signed integers to avoid unexpected bugs, but many others (including myself) disagree that's a good way of going about it since, as you said, it's not guaranteed to trap or do anything to help the programmer.
- aidenn0 5y agoI've never seen signed-integers being required. They tend to introduce unexpected bugs rather than prevent it. Of course you can accidentally end up with signed integers when you didn't intend it. Here's my favorite accidental undefined signed integer overflow, assuming 32-bit integer size (the 64-bit version is similar) uint32_t foo(uint8_t x) { return x << 24; } Yes, this is signed integer overflow, since x gets upgraded to a signed integer before the shift, if an integer is 32-bits in size, this can result in undefined behavior if the top bit of X is set. Fortunately I've never seen a compiler optimize this to stupidity.
- junon 5y agoIf memory serves, the LLVM Project prescribes signed integers be used in all cases except where unsigned is mandatory.