On Mon, 20 Feb 2012 11:22:32 -0500, a <[email protected]> wrote:
auto a = [1,2,3,4,5];
auto b = a[0..3];
assumeSafeAppend(b);
b ~= 0;
writeln(a);
prints [1, 2, 3, 0, 5], so b is not reallocated in this case.
Just to clarify: this is not guaranteed to print [1, 2, 3, 0, 5], it
only does
if there is still enough memory in a block allocated to b and it doesn't
have
to be reallocated.
In fact, it is guaranteed. a is guaranteed to be allocated on the heap,
because that's what an array literal does (currently, but it probably
shouldn't be). It's guaranteed to be inserted into a block large enough
to hold 5 numbers, so you definitely can put 4 numbers (1, 2, 3, 0) into
it. This will never reallocate.
However, It's not guaranteed that the last number will be 5 after
assumeSafeAppend is called. Once you call assumeSafeAppend, all data
after that last element is invalid (i.e. not used). To refer to that data
is implementation-defined. E.g. a different version of assumeSafeAppend
may set all invalid bytes to 0, or some other sentinel value.
-Steve