3 ms·
Little nitpick: In C/C++ it's "only" UB for signed ints. For unsigned ints it's arithmetic modulo 2^n (where n is the number of bits in the value representati
by still_grokking 4y ago
Little nitpick:
In C/C++ it's "only" UB for signed ints.
For unsigned ints it's arithmetic modulo 2^n (where n is the number of bits in the value representation of that particular size of integer).
Such divergent definitions make the whole mess of course even bigger, that's not the point.
- pratk 4y agoAlso, the UB can be mitigated with -fwrapv (if desired) to handle overflowing integers with 2's complement
- still_grokking 4y agoStupid question, but how can a compiler flag "mitigate" some UB that is (to my knowledge) defined by the standard as UB? This would be possible only if the standard would define the behavior in question as implementation defined. But that's not the case afaik. Signed overflow is undefined behavior as it's afaik defined to be that. But maybe I'm just wrong here? But at least SO is confining my knowledge: https://stackoverflow.com/questions/16188263/is-signed-integer-overflow-still-undefined-behavior-in-c https://stackoverflow.com/questions/16188263/is-signed-integ...
- pratk 4y ago-fwrapv forces the compiler to handle signed overflow with 2's complement. UB essentially means the compiler is free to do anything, and with the option it chooses the wrapping behavior. See https://stackoverflow.com/questions/47232954/what-does-fwrapv-do https://stackoverflow.com/questions/47232954/what-does-fwrap...