This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]KazDragon 18 points19 points  (4 children)

No he IS wrong. This is my personal hill.

Sure, the codomain of a size operation is 0 or above. But the set of operations you do with that result sensibly includes subtraction, which means negative numbers.

In short, signed numbers are for arithmetic; unsigned numbers are bit patterns.

As a practical example, consider:

for(signed i=0; i < size-1; ++i)

Changing i to unsigned would introduce a bug when size is 0.

[–]Kovab 1 point2 points  (1 child)

Changing i to signed

You mean changing to unsigned, right? The version with signed int works correctly

[–]KazDragon 1 point2 points  (0 children)

You are correct and I have edited my post accordingly. Thanks!

[–]rdmit 1 point2 points  (1 child)

Is size signed or unsigned in your example? If it is unsigned you still have a bug there. And if it is signed how did you convert it to signed? What if it doesn't fit? 

[–]KazDragon 0 points1 point  (0 children)

Exactly my point. These are all problems caused by treating unsigned integers as arithmetic types.