Confusion over what types have value vs reference semantics
Mike Parker via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Sep 11 09:14:59 PDT 2016
On Sunday, 11 September 2016 at 16:10:04 UTC, Mike Parker wrote:
> And here, no memory is allocated. barSlice.ptr is the same as
> bar.ptr and barSlice.length is the same as bar.length. However,
> if you append a new element:
>
> barSlice ~= 10;
>
> The GC will allocate memory for a new array and barSlice will
> no longer point to bar. It will now have four elements.
I should clarify that this holds true for all slices, not just
slices of static arrays. The key point is that appending to a
slice will only allocate if the the .capacity property of the
slice is 0. Slices of static arrays will always have a capacity
of 0. Slices of slices might not, i.e. there may be room in the
memory block for more elements.
More information about the Digitalmars-d-learn
mailing list