r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
Blog post Unsigned Sizes: A Five Year Mistake
https://c3-lang.org/blog/unsigned-sizes-a-five-year-mistake/
91
Upvotes
r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
View all comments
Show parent comment
1
u/Tasty_Replacement_29 Bau May 05 '26 edited May 05 '26
OK, here are few edge cases and security issues:
int n = 100; for (unsigned i = n - 1; i >= 0; i--) { printf("%d\n", i); }. It is quite tricky to implement this type of loop correctly.int a = -1; unsigned int b = 1; if (a < b) printf("a is less than b\n"); else printf("b is less than a\n");If you need to think about it, then you have already an issue: It should be obvious without thinking.int *ptr = malloc(count * sizeof(int));because the allocated size might be too small, without error. With signed, it would be easy to catch the problem.x > aorx >= aorx < byou have an edge case of underflow / wraparound, which you don't have if you use signed.How do you do that? You can not use
>= 0.For example, operations where you have both signed and unsigned. Do you disallow it, or do you log a warning, or what do you do? This is specially tricky if you use type inference:
i := 1is this signed or unsigned? Whatever you pick, you can then have a challenge if you mix unsigned with signed values.Signed integer also have overflow and underflow, but the edge cases are much, much farther away than for unsigned integers, where they are very close to a value that is used very often: just next to
0. Unsigned underflow is near everyday values, and signed overflow is near extreme values.BTW thanks for writing; I think I know understand why my comment was downvoted: I didn't write it clear enough. I hope the above is more clear, if not, please let me know!