D array expansion and non-deterministic re-allocation

Ali Cehreli acehreli at yahoo.com
Mon Nov 16 00:45:04 PST 2009


Bartosz Milewski Wrote:

> I read Andrei's chapter on arrays and there's one thing that concerns me.

I tried to voice the same concern earlier, but my voice is not loud yet. ;)

> When a slice is extended, the decision to re-allocate, and therefore to cut
> its connection to other slices, is non-deterministic.

I don't see it being any different than C++'s std::vector invalidating iterators when the vector is expanded. That has the same "non-determinism", but we've been living with it.

The reason we've been living happily with it is, because it's been spelled out: "may invalidate all iterators."

In D2's slices, what we're missing is the correct language to describe the semantics. As long as we set the semantics right, there will be no problem. (Earlier, I proposed "discretionary sharing semantics" for D2's slice semantics.)

The first code examples that are used to describe slices better say "slice" on the left hand side:

  int[] slice = new int[2];  // GOOD for the semantics
  int[] array = new int[2];  // BAD for the semantics

Ali




More information about the Digitalmars-d mailing list