Integer overflows
Nick Sabalausky
a at a.a
Sun Jul 6 14:51:35 PDT 2008
"superdan" <super at dan.org> wrote in message
news:g4r88k$qjj$1 at digitalmars.com...
> bearophile Wrote:
>
>> A short article from July 01, 2008, shows that some people are
>> re-inventing the integer overflows compiler check that Pascal/Delphi
>> programmers have enjoyed for lot of years:
>>
>> http://www.ddj.com/hpc-high-performance-computing/208801981
>>
>> This time I am happy that history is repeating itself.
>
> me 2 but the article is just a rant. we should demand this from languages
> and computers. well yeah i demand a cow with diet coke too.
>
> i was always unclear on somethin'. should the check be in the operation or
> the type of the operand? i mean maybe i have some data and only sometimes
> i want to make sure there's no overflow. so i'm sayin'
>
> int a, b, c;
> a = b _+_ c;
>
> or something. that _+_ is checked and shit. or should i be like
>
> Safe!(int) a, b, c;
> a = b + c;
>
> arguments could go both ways i guess. the advantage of the albeit uglier
> first solution is that it is more likely to be used. somehow safe types
> keep on comin' in languages but people don't use'em if thery're not the
> default. positive bias i suppose. (ppl underestimate the likelihood of bad
> shit happening to'em and overestimate their capacity to avoid bad shit
> when it is imminent.)
C# has a nice solution that involves (I think this is what they're named:)
"checked" and "unchecked" keywords. (Heh heh, yes, here I go praising
something in C# again. I really do like D better overall, honest! (Of
course, D is helped by the fact that C#'s templates are effectively gimped -
just look up the whole "no IArithmetic" bizzareness. Anyway...))
The way it works is this:
In the compiler settings, you can choose a default, either artithmetic is
checked for overflows by default or not checked by default. Then, in the
code, you can override the compiler setting by doing something like this:
checked
{
// Code here
}
unchecked
{
// Code here
}
a = checked(b + c);
a = unchecked(b + c);
More information about the Digitalmars-d
mailing list