Array start index

Andrej Mitrovic via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 1 12:04:00 PDT 2015


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.


More information about the Digitalmars-d-learn mailing list