2 ms·
Frankly what I think it we should have a compramize. Make overflow on a signed int defined. Create a new type called unsafe_int where it is not. So then the c
by Gibbon1 4y ago
Frankly what I think it we should have a compramize.
Make overflow on a signed int defined.
Create a new type called unsafe_int where it is not.
So then the compiler writers get to implement their pointless optimization pit traps. And everyone else can avoid those by banning unsafe_int.
- eru 4y agoYou can already compile with `-fno-strict-overflow` to get twos-complement overflow for signed integers. I meant to talk more abstractly about UB in general.
- astrange 4y agoThe signed integer overflow rule is extremely important for common optimizations, mostly related to loops like knowing if they're finite or rewriting their index directions. The way to start getting rid of it would be to add for...in... loops or something where the loop index can be a custom no-overflow type. And "defining" it is a lame approach to safety. If you make it wraparound, you now have silent wraparounds that can't be found by static analysis. You want unintended overflows to trap, not just be defined.
- eru 4y ago> And "defining" it is a lame approach to safety. If you make it wraparound, you now have silent wraparounds that can't be found by static analysis. You want unintended overflows to trap, not just be defined. Yes. But even the lame approach is better than UB, because it doesn't bring the whole program down.
- Gibbon1 4y agoI've been wondering if I should mention that using int for an index is a bad idea because the standard only guarantees it's 16 bits. You should use size_t instead. And in C size_t is unsigned. My take is all of the low hanging fruit optimizations that the standard enables has been picked a long time ago. Everything left is problematic.