Slice allocation after appending

Basile B. b2.temp at gmx.com
Wed Dec 23 02:58:54 UTC 2020


On Tuesday, 22 December 2020 at 22:12:29 UTC, Rekel wrote:
> 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?

No there's some runtime and GC magic under the hood. Appending on 
the slice is more like a smart "copy on append" operation so "a" 
will always ends with -1 and "b" with 2. It's described here : 
https://dlang.org/articles/d-array-article.html#how-it-works


More information about the Digitalmars-d-learn mailing list