3 ms·
Regehr, where this post is from, recently had an article about how to do safe arithmetic in C. http://blog.regehr.org/archives/1139 http://blog.regehr.org/arch
by TorKlingberg 12y ago
Regehr, where this post is from, recently had an article about how to do safe arithmetic in C.
http://blog.regehr.org/archives/1139 http://blog.regehr.org/archives/1139
As you say, it is surprisingly different to it get right, especially if you want good performance. A common mistake is checking for signed integer overflow after the fact. It is too late when undefined behavior has already happened.
- userbinator 12y ago> It is too late when undefined behavior has already happened. This seems more like a theoretical concern, as just about all the hardware out there is 2's complement and signed overflow wraps around in the usual way - because that's the simplest, most straightforward way to implement it. (This also means I think compilers that exploit this "loophole" in the language are seriously violating the principle of least surprise.) If you're working on the few exceptions to this, then integer overflow is probably going to be the least of your worries...