Earlier quoted context omitted.
But "unsigned" doesn't actually enforce the semantics you want. Missing an overflow check means your value will never be negative, but it is almost certainly still a bug. And because unsigned overflow is defined, the compiler isn't allowed to prevent you from doing it! This is just enough type semantics to injure oneself.
So, because its not perfect, should you throw it all out?
Summary of C/C++ integer rules
91–98 of 98 posts
Re: Summary of C/C++ integer rules
#92In the myths section: > char is always 8 bits wide. int is always 32 bits wide > Signed overflow is guaranteed to be wrap around. (e.g. INT_MAX + 1 == INT_MIN.) Are there any current, relevant hardware architectures where this is not true (e.g. bytes are not 8 bits, and integers are not 2's complement)? E.g. what's the point of "portability" if there is no physical hardware around anymore where those restrictions wou…
There are loads of DSPs, MCUs, and other non-PC junk where CHAR_BIT is not 8. For example of the SHARC, CHAR_BIT is 32, absolutely every type is 32 bits wide.
Re: Summary of C/C++ integer rules
#93Earlier quoted context omitted.
So, because its not perfect, should you throw it all out?
No. Because people tend to make more mistakes if they try to use unsigned values in this manner in C/C++.
Re: Summary of C/C++ integer rules
#94Earlier quoted context omitted.
No. Because people tend to make more mistakes if they try to use unsigned values in this manner in C/C++.
I’ve personally never encountered a bug that turned out to be caused by an unsigned value. YMMV, I guess.
Re: Summary of C/C++ integer rules
#95[Dons language lawyer hat] > floating-point number types will not be discussed at all, because that mostly deals with how to analyze and handle approximation errors that stem from rounding. By contrast, integer math is a foundation of programming and computer science, and all calculations are always exact in theory (ignoring implementations issues like overflow). Integer overflow is no mere implementation issue, any…
>> Character literals (in single quotes) have the type (signed) int in C, but (signed or unsigned) char in C++. > That's not correct. In C++, the type of a character literal is simply char, never signed char nor unsigned char. I'd assume the author meant (signed `char` | unsigned `char`) rather than (`signed char` | `unsigned char`).
Re: Summary of C/C++ integer rules
#96Earlier quoted context omitted.
Positive values are a particular case of signed values, you can still use signed ints to store positive values. No need to enforce your semantics through type, and especially not when the values of the type are trivially particular cases of the values of another type. For example, when you write a function in C that computes prime factors of an int, do you need a type for prime numbers? No, you just use int. The same…
> Positive values are a particular case of signed values, you can still use signed ints to store positive values. And yet Java's lack of unsigned integers is considered a major example of its (numerous) design errors. > No need to enforce your semantics through type, and especially not when the values of the type are trivially particular cases of the values of another type. Of course not, there's no need for any type…
Re: Summary of C/C++ integer rules
#97One thing I think should have been mentioned: size_t is guaranteed to be large enough to index all of memory, which is why it is the return type of size of.
size_t is only guaranteed to be large enough to store the size of the largest object. This is not the same as being able to index all of memory. You could imagine a platform with restricted continuous allocation size where the maximum object size is smaller than the size of the address space.
Re: Summary of C/C++ integer rules
#98It’s funny that you would end up with a similar conclusion for other parts of the language (e.g. operators) as well. Just a gigantic set of inane rules everywhere causing you to constantly be in danger of introducing bugs and portability issues.
It's more funnier that although language is full of traps, in practice it works quite well. I don't think any C developer(or let's say %95) knows all the rules mentioned in the article, yet we are still one piece. Does anybody know any paper for bugs per lines of code for different languages or something similar?