Partial arrays reclaimed?
Ivan Kazmenko via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 31 06:24:28 PST 2017
On Friday, 27 January 2017 at 23:22:17 UTC, Nick Sabalausky wrote:
> Suppose an array is being used like a FIFO:
>
> -----------------------
> T[] slice;
>
> // Add:
> slice ~= T();
>
> // Remove:
> slice = slice[1..$];
> -----------------------
>
> Assuming of course there's no other references to the memory,
> as this gets used, does the any of the memory from the removed
> elements ever get GC'd?
As I see it, the line
slice = slice[1..$];
effectively ends slice's in-place appending capabilities. So
each append after remove will likely reallocate. You have to use
assumeSafeAppend to re-enable appending in place.
Here [1] is an old thread about the caveats of using built-in
arrays as queues and stacks. If not in a hurry, the better
option is perhaps to just write the respective wrapper structs
which explicitly store indices, instead of using built-in slices
and assumeSafeAppend all over the place.
Ivan Kazmenko.
[1]
http://forum.dlang.org/post/yrxspdrpusrrijmfyldc@forum.dlang.org
More information about the Digitalmars-d-learn
mailing list