On Tue, 09 Mar 2010 10:10:10 -0500, biozic <[email protected]> wrote:
Steven Schveighoffer Wrote:
* shrinkToFit(T[] arr): This one is a bit tricky and technically
unsafe.
It reduces the size of the "allocated" length to fit the length of the
array. The allocated length is the length that the runtime assumes is
being used of the memory block. This is what aids in preventing
stomping,
so use with care. You can achieve stomping if you use shrinkToFit on a
slice, but still have references to the trimmed data. Example:
string s = "12345".idup;
string slice = s[0..2];
slice.shrinkToFit(); // tells the runtime that you will no longer use
any data beyond the slice
slice ~= "00"; // appends in place, overwriting immutable data
referenced
Actually, slice.capacity *increases* from 0 to s.capacity when calling
shrinkToFit, doesn't it? So "stomp" or "prepare_for_stomping" could be a
better name.
Yes, it is technically like that. shrinkToFit really looks weird as a
property of the slice you are shrinking to because it really is operating
on the array data itself (which has no concrete type or reference).
I don't like anything that talks about stomping, because we are not trying
to say this is a stomping operation. I want to focus more on the fact
that you are declaring the data after the slice as being no longer used.
It's definitely a harder function to name...
-Steve