4 ms·
The argument is really strange. It says that unsigned ints can't become negative and it's a good thing. But they do that by instead having crazy high values. Ho
by whattheactualf3 5y ago
The argument is really strange. It says that unsigned ints can't become negative and it's a good thing. But they do that by instead having crazy high values. How is that a win?
You could argue that it doesn't matter because an incorrect program is incorrect regardless. True. At least with signed integers you can represent differences between positive values. How do you do that with unsigned?
- ridiculous_fish 5y agoSigned integers may also have crazy high values so you're already on the hook for dealing with those. The case for unsigned values is that it frees you from having to think about negative inputs. We all know to check for divide by zero; how many remember to check for INT_MIN / -1? Regarding differences, in practice you usually want those differences to be non-negative. With uints, you compare first. Not: int length = end - begin; if (length < 0) return error; but: if (begin > end) return error; unsigned length = end - begin; This is also a good habit for signed ints, since it avoids underflow in more (but not all) cases. An old post with a few fun crashes for negative inputs: https://kqueue.org/blog/2012/12/31/idiv-dos/ https://kqueue.org/blog/2012/12/31/idiv-dos/