2 ms·
Well, what I really want is a trap on overflow, but since C doesn't support that, modern architectures don't have the capability; you can do it in software (lik
by aidenn0 3y ago
Well, what I really want is a trap on overflow, but since C doesn't support that, modern architectures don't have the capability; you can do it in software (like with ubsan), but you pay a performance penalty.
- ndiddy 3y agoMost popular C compilers support trapping on integer overflow (i.e. https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-ftrapv https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#ind...), but it does have some overhead as it replaces the native arithmetic instruction with a call to a library function (i.e. https://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html#Trapping-arithmetic-functions https://gcc.gnu.org/onlinedocs/gccint/Integer-library-routin...)
- matheusmoreira 3y agoI mentioned the -fwrapv option to make it wrap around. There's also a -ftrapv option that makes the compiler generate traps.