Stack-based @nogc dynamic array

Marco de Wild mdwild at sogyo.nl
Fri May 17 06:59:11 UTC 2019


On Thursday, 16 May 2019 at 12:45:03 UTC, Adam D. Ruppe wrote:
> I think you have overcomplicated something quite simple.
>
> int[4] buffer;
> int bufferLength;
>
> buffer[bufferLength++] = item_to_append;
> buffer[bufferLength++] = item_to_append;
>
> int[] slice = buffer[0 .. bufferLength];
>
> // you can use slice to any std.algorithm calls etc
> // just remember it is on the stack so don't store it beyond a 
> function call

Thanks. It's really a lot simpler than I thought. It's slightly 
error prone (i.e., the code doesn't work if I use 
++bufferLength), but its simplicity might be worth the trade-off.


More information about the Digitalmars-d-learn mailing list