Dynamic array as stack and GC.BlkAttr.APPENDABLE

IgorStepanov via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 14 14:16:38 PST 2014


Recently I encountered the following problem.
I need a simple stack of uint.
I want to push uints back and pop it. I don't want to copy this 
stack and I want it to work fast.

In a first approximation, the problem seems easy.

uint[] my_stack;
my_stack.reserve(256);

my_stack ~= 1; //push
uint head = my_stack[$ - 1]; //top
my_stack.length--; //pop
my_stack ~= 2; //push

However, when I changes the length and append new elem to the 
array after it, array is reallocated.

Ok, good point. Seems reasonable. I might create slice of array 
copy before changing of the length. And by default changing array 
length or creating slice should cause an array reallocation at 
appending.

However I don't plan to copy or creating slices of this array, 
thus I need to some magic for avoid reallocation.

I've found a GC.BlkAttr.APPENDABLE flag at core.memory.

How ever it hasn't helped me.
Is there another way to do it without manually managing of memory?


More information about the Digitalmars-d mailing list