No bounds checking for dynamic arrays at compile time?

bearophile bearophileHUGS at lycos.com
Thu Dec 13 02:40:59 PST 2012


Walter Bright:

>> void main() {
>>     int[5] x;
>>     x[$] = 1;
>>     enum size_t n = 2;
>>     x[$ + n] = 2;
>> }
>
> The compiler does that already.

I am compiling that little program with

dmd -w test.d

And I see no compilation errors.

See also here, it gives a run-time error:
http://dpaste.dzfl.pl/a62a10aa


>> void main() {
>>     int[] x = new int[5];
>>     x[$] = 1; // easy
>>     x[x.length] = 1; // idem
>>     enum size_t n = 2;
>>     x[$ + n] = 2; // not too much hard if n is unsigned
>>     x[x.length + n] = 2; // idem
>> }
>
> I just don't see the point in adding flow analysis for that,

I think that doesn't require flow analysis.


> and it'll ding you at runtime anyway.

This thread is about spotting mistakes at compile-time, that is 
one of the main advantages of having a static typing in the first 
place.

Bye,
bearophile


More information about the Digitalmars-d mailing list