Null references (oh no, not again!)

Walter Bright newshound1 at digitalmars.com
Wed Mar 4 03:29:50 PST 2009


bearophile wrote:
> Walter Bright:
>> 3 [Integer overflow] is a problem, but fortunately it tends to be
>> rare.
> 
> There are ways to avoid them, and it seems C#, Delphi, FreePascal,
> and LLVM designers don't agree with you. A nice small post on the
> topic: 
> http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html


There is a SafeInt class built for C++. It should be quite doable for D 
without needing any particular language support. That kind of thing is 
precisely what operator overloading is for. The nice thing about it is 
anyone can write and use such a class - no need to convince anyone else 
of its merits.

Or you could change the compiler to throw an exception on any integer 
arithmetic overflow. Sounds great, right? Consider that there's no 
hardware support for this, so the following would have to happen:

regular code:
     add EAX,EBX

checked code:
     add EAX,EBX
     jc Overflow

This is going to slow things down and bloat up the code generation. But 
wait, it gets worse. The x86 has a lot of complex addressing modes that 
are used for fast addition, such as:

     lea EAX,[EBX*8][ESI]

None of these optimizations could be used if checking is desired.

So, to keep the performance, you'll have to be able to select which one 
you want, either by a separate parallel set of integer types (doubling 
the number of types), or by having special code blocks, such as:

     checked  // this is what C# does
     {
             x = a + b;
     }

I just don't see that being very popular. Code is full of arithmetic, 
and adding checked all over the place will not only uglify the code, 
chances are nearly certain that it will get omitted here and there for 
operations that might overflow.



More information about the Digitalmars-d mailing list