Change representation of dynamic arrays?

Janice Caron caron800 at googlemail.com
Sat Oct 20 07:41:00 PDT 2007


On 10/20/07, Jascha Wetzel <firstname at mainia.de> wrote:
> > Yes, an extra byte is allocated for just this reason.
>
> couldn't the end pointer just point to the last element?
> iteration could use <=

Ooh - just noticed this.

The test is normally (p != end). You can't use <= with iterators in
general because, if you did, the iterators would have to implement
opCmp(), which would be an O(N) operation in the case of linked lists.
!= is O(1).

With plain old arrays, for (p=start; p<=end; ++p) would fail with
empty arrays if the empty array was represented as [ null, null ],
because (null <= null) is true, so the for body would get executed
once with p == null. (!)

So there are /lots/ of reasons why end has to point /beyond/ the last byte.



More information about the Digitalmars-d mailing list