Dynamic Arrays as Stack and/or Queue

mipri mipri at minimaltype.com
Tue Oct 8 20:42:22 UTC 2019


On Tuesday, 8 October 2019 at 10:48:45 UTC, Jonathan M Davis 
wrote:
> The result of this is that code like
>
> stack.popBack();
> stack ~= foo;
> stack ~= bar;
> stack.popBack();
> stack ~= baz;
>
> will end up allocating all over the place. Every time you
> append to the array after shrinking it, you're going to end up
> with the GC allocating a new block of memory instead of
> appending in place.
>

Thanks as well! I thought the code I posted would only allocate 
when
the array ran out of capacity. And I see how, even if that's 
worked
around with assumeSafeAppend, it becomes a bad idea to define the
stack functions against `ref T[]` as this makes it too easy for 
other
code to slice the array and cause the bugs that assumeSafeAppend
allows.


More information about the Digitalmars-d-learn mailing list