7 ms·
"This claim that positive signed overflow wraps around is neither correct by the C standard nor consistent with the observed behavior of either GCC or LLVM. Thi
by foyk 10y ago
"This claim that positive signed overflow wraps around is neither correct by the C standard nor consistent with the observed behavior of either GCC or LLVM. This isn’t an acceptable claim to make in a popular C-based textbook published in 2015."
Perhaps someone could explain what I'm missing. It's exactly the behavior that I see using gcc-4.8 and Apple llvm-7.3.
- strcat 10y agoRead the linked content in the post about undefined behavior. Signed overflow is undefined, not implementation defined. Clang and GCC treat it as undefined such without -fwrapv. That means they assume it cannot happen and feed information into optimization passes and code generation based on that assumption. It's worse than the result potentially being different: a program with signed overflow may crash, corrupt data or worse and it happens in practice. One common example is overflow checks often being optimized out if they do it by trying the operation and then checking for overflow. As the compilers get smarter, the problems will grow. They barely do any integer range analysis right now...