Disallow arrays as pointers

bearophile bearophileHUGS at lycos.com
Sun Oct 30 17:13:25 PDT 2011


Timon Gehr:

> I have never had a bug because of arrays implicitly converting to 
> pointers.

Good.


> A lot of C's carefully designed syntactic elegance is 
> lost when going from pointers/iterators to arrays and ranges.

You see that D design tries hard to avoid the need to use pointers as much as possible, because pointers are bug prone, give less readable code, and they are not needed in most cases (you use something a bit higher level, like arrays or references).
Some people say that C language is as elegant as a katana sword, but I have to say that most times you want to keep it in its sheath :-)


> But why is *array not bounds checked in non-release mode? I think it should be.

In D there are arrays, and there are pointers. Arrays are bound-checked, pointers aren't. So when you use arrays as pointers, or when you use somearray.ptr[x] you have lost array bounds. Adding a third intermediate type is not a good idea, it increases language and implementation complexity for a practice that is meant as discouraged since the introduction of the ptr field.

Bye,
bearophile


More information about the Digitalmars-d mailing list