Array start index

bachmeier via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 1 16:02:50 PDT 2015


On Saturday, 1 August 2015 at 19:04:10 UTC, Andrej Mitrovic wrote:
> On 8/1/15, DLearner via Digitalmars-d-learn 
> <digitalmars-d-learn at puremagic.com> wrote:
>> D is a C derivative, so it seems a shame not to identify causes
>> of bugs in C,
>> and design them out in D.
>
> This has already been done! D defines an array to be a struct 
> with a pointer and a length. See this article: 
> http://www.drdobbs.com/architecture-and-design/cs-biggest-mistake/228701625
>
> I would argue it's not "off-by-one" that's causing most issues 
> when dealing with C "arrays", but instead it's in general 
> out-of-bounds issues (whether it's off bye one or off by 50..) 
> since you often don't have the length or could easily use the 
> wrong variable as the length.
>
> Think about how much D code would actually have subtle 
> off-by-one errors if D didn't use 0-based indexing like the 
> majority of popular languages use. Any time you would interface 
> with other languages you would have to double, triple-check all 
> your uses of arrays.
>
> FWIW at the very beginning I also found it odd that languages 
> use 0-based indexing, but that was before I had any significant 
> programming experience under my belt. By now it's second nature 
> to me to use 0-based indexing.

But what type of programming are you doing? Even after decades of 
programming and trying out dozens of languages, zero-based 
indexing still gets me at times when the arrays I work with 
represent vectors and matrices. Especially when porting code from 
other languages that use one-based indexing. One of the nice 
things about D is that it gives you the tools to easily make the 
change if you want.


More information about the Digitalmars-d-learn mailing list