Array bound checks removal increasing importance

Wanderer via Digitalmars-d digitalmars-d at puremagic.com
Sat May 31 05:49:08 PDT 2014


> void main() {
>     int[5] data;
>     foreach (const i; 0 .. 10)
>         data[i] = 0;
>     foreach (immutable i; 0 .. 10)
>         data[i] = 0;
>     int[10] big;
>     foreach (const i, x; big)
>         data[i] = x;
> }

I'm not sure if bound checks should be removed here. Before 
removal, this code gives safe runtime exception, or, as suggested 
above, compile-time error. After removal, this code might cause 
access violation - which, unlike runtime exception, would leave 
program/kernel in corrupted state.

>
>
> But the compiler must recognize this as correct code:
>
>
> void main() {
>     int[5] data;
>     foreach (const i; 0 .. 10)
>         if (i < 5)
>             data[i] = 0;
> }

My personal opinion is that code like this should remain 
inefficient, to stimulate programmers to use simpler, easier to 
understand idioms, like foreach (i; data). If bound checks get 
removed in this case, that already covers 90% of loops under 
question. :-)


More information about the Digitalmars-d mailing list