3 ms·
D got around the overflow-is-UB problem by declaring that 2s-complement arithmetic will be used with wraparound semantics. Is there any reason for modern C to
by WalterBright 5y ago
D got around the overflow-is-UB problem by declaring that 2s-complement arithmetic will be used with wraparound semantics.
Is there any reason for modern C to still support anything else?
- mhh__ 5y agoThere are still processors that don't use two's complement, although I'm not sure that should really stop them if they wanted to declare all C implementations must be t-c.
- WalterBright 5y agoThose processors can use a dialect of C that embraces ones-complement. Mandating such support in the Standard has little practical effect on code, I expect most programs would have to be recoded to work on ones-complement anyway.
- zajio1am 5y agoInteger overflow is usually logic error, so a reasonable default behavior would be a trap instead of silent overflow (regardless of how signed integrers are stored in memory). Some architectures support that (e.g. MIPS).
- WalterBright 5y agoThe article mentions cases where integer overflow is expected behavior.
- zajio1am 5y agoC already offers 'unsigned' type variants that offers defined overflow (together with non-negative range). It would be useful to have another type variant that offers defined overflow (like in unsigned) together with signed range for such cases. But it still makes sense for basic integers to have overflow as UD, as in most cases it is not expected behavior. Note that in current C, if one needs defined overflow on signed integers, one can cast them to unsigned, to the operation and cast result back to int. That makes it implementation-defined instead of undefined.
- WalterBright 5y ago> as in most cases it is not expected behavior Yeah, but the gotcha happens when it is expected behavior.
- nwmcsween 5y ago-fsanitize-undefined-trap-on-error
- gpderetta 5y agoGuaranteed 2 complement arithmetic for signed integers was recently added to C++. Wrap around is still UB though because apparently caused regressions in some significant code bases. Guaranteed order of evaluation of arguments almost made it into the standard, but because of regressions, we didn't quite get the full benefits; for example: i = i++ + 2; is now fully defined, while this: f(++i, ++i); is no longer UB, but implementation defined. Ideally for every UB taken away we would get one or more pragmas to get the optimization back like ivdep. The issue is that that doesn't help old code bases.
- WalterBright 5y agoD also guarantees left to right evaluation order. > The issue is that that doesn't help old code bases. D has a flag called -revert=issue where old behaviors will be supported for a time.
- uecker 5y ago(first, I am not a D user but I really like what I have seen. I wish WG14 would take more inspiration from D than from C++) C23 will require 2s-complement. Signed overflow is still UB in C. I think this is a better choice than wrap around, because overflow is often a bug. With UB, you can use static analysis (to some degree) or run-time traps (if the compiler supports this) and then fix those bugs. If it were defined to wrap around, those bugs are much harder to find.