Slice allocation after appending

Rekel paultjeadriaanse at gmail.com
Tue Dec 22 22:12:29 UTC 2020


According to the D slice article 
(https://dlang.org/articles/d-array-article.html), slices do not 
care where they start, only where they end, when checking whether 
expanding in place is permitable, or at least that is what I 
understood regarding it.

Now I'm unsure how to check this, I tried to a bit using the 
online editor and a bit of pointer usage which seemed to confirm 
my suspicion, but does this mean that taking a (small) slice at 
the end of a (possibly) very large dynamic array can lead to 
problematic behavior?
For example;

> int[] a = new int[10]; // Imagine this is very large
> int[] b = a[$-1..$];   // Small slice at the end
> b ~= 2;                // b extends, possibly in place
> a ~= -1;               // a no longer can, so the entire array 
> needs reallocating
(instead of be reallocating & a growing in place)

Again I'm not very certain I fully understood how slices are 
implemented, but is this example, and the problem I imagine it 
leading to, valid?


More information about the Digitalmars-d-learn mailing list