On Sunday, 11 September 2016 at 16:14:59 UTC, Mike Parker wrote:
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.
Thanks for the detailed answer. I still don't get the advantage
of passing slices into functions by value allowing modification
to elements of the original array. Is there an way to specify
that a true independent copy of an array should be passed into
the function? E.g, in c++ func(Vector<int> v) causes a copy of
the argument to be passed in.