Notes IV

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Jan 24 06:01:24 PST 2008


bearophile wrote:

> 16) Against C rules, in some situations I think it may be better if some results are upcasted to ulong/long:
> import std.stdio;
> void main() {
>   uint a = 3_000_000_000;
>   uint b = 3_000_000_000;
>   writefln(a + b); // 1705032704
> 
>   int c = -1_600_000_000;
>   int d = -1_600_000_000;
>   writefln(c + d); // 1094967296
> }
> (The Ada language uses a different solution to avoid such class of bugs, but it may be too much far from the style of C-like languages. Delphi looks like a compromise).
> 

On a similar topic, here are two of my favorite bugs:


// Spot the bug # 1
double randomDelta() {
     return (rand() % 3) - 1;
}

// spot the bug #2
void fun(int[] arr) {
     long d = arr.length - 10;
     while (d > 0)
         arr[--d] = 0;
}

-- 
Oskar



More information about the Digitalmars-d mailing list