D array expansion and non-deterministic re-allocation

Ali Cehreli acehreli at yahoo.com
Sat Nov 21 22:28:58 PST 2009


dsimcha Wrote:

> == Quote from Bartosz Milewski (bartosz-nospam at relisoft.com)'s article
> > > int[] a = [0];
> > > auto b = a;
> > > a ~= 1;
> > > b ~= 2;
> > > What is a[1]?
> > >
> > > Is this considered "stomping" and requiring a re-allocation?
> > Can this question be answered without the recourse to implementation, and the
> MRU cache in particular?
> > I thought about an abstract definition of "stomping".Something like that: The
> expanded part of the array is never implicitly shared with any other array. But
> I'm not sure about this case:
> > int[] a = [1, 2, 3];
> > int[] b = a[0..1];
> > b ~= 4;
> > what is a[1]?
> > By my definition, this would be considered stomping, but I couldn't find this
> example in TDPL.
> 
> a[1] == 2.  The MRU cache stores both the pointer and the length.  When you
> attempt to append to b, the MRU is searched.  No array that starts at b.ptr and
> has length b.length is found.  b is reallocated.

Consider there is also c:

  int[] c = b[0..1];

According to the above definition, after b~=4 'b' would be relocated and b[0] would be disjoint from c[0].

Now consider that a's definition is changed to just [ 1 ].

If I understand this correctly, then b.ptr and b.length would match the original array, and in this case b ~= 4 operation might not relocate b, and b[0] and c[0] would refer to the same element.

A change in 'a' would be defining the relation between b and c.

Ali




More information about the Digitalmars-d mailing list