2 ms·
The pain in C is that it lacks any features for dealing with overflow properly, and has a few other features that even work against it. If you check for overfl
by pornel 5y ago
The pain in C is that it lacks any features for dealing with overflow properly, and has a few other features that even work against it.
If you check for overflow in a slightly wrong way, you'll accidentally "prove" to the compiler that the overflow check can be optimized out entirely. There's no explicit saturating or checked arithmetic. Even when you use only unsigned types, you still need to be paranoid about implicit integer promotion that can make intermediate operations signed.
These things could be fixed with relatively small changes to the spec or stdlib, but I don't think there's a will for it among C users.
- tialaramex 5y agoI think saturation might actually have more takers, because I think almost everybody who knows they want saturation arithmetic would jump at saturated_int16_t or whatever, whereas too many people who should have checked arithmetic don't even realise it's what they need. But I agree with you these wouldn't be difficult to implement and are worth the appropriate committee looking at so that at least the programmers who realise they have a problem also have a solution. C like C++ and Ada standards are actually a product of a JTC1 sub-(sub-?)-committee and so it's not so much about the will of C users in general as it is about the will of advocates to justify this to that committee and push it through all the bureaucracy. Anyone actually interested in this should definitely engage with the people trying to get stuff done for C++ because the two languages know that they both benefit from the relatively good mutual compatibility, so neither would appreciate a pointless divergence on this work.
- sesuximo 5y agoWhile not standardized, there are compiler builtins for operations which care about overflow. They will not be optimized out and are very efficient (cost ~1 cycle if no overflow).
- rwmj 5y agoI had to use them when I was formally proving some C snippets (see my other comment: https://news.ycombinator.com/item?id=27649879 https://news.ycombinator.com/item?id=27649879)