Local fixed sized arrays

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 27 15:56:35 PDT 2016


On 06/27/2016 02:58 PM, Smoke Adams wrote:
> I'm in need of a way to create a local array that isn't GC'ed. It must
> be dynamic in the sense of setting the size at compile time but it will
> be used only in scope and only on structs.
>
> function x(int y)
> {
>     bool[y] arr;
>
>     arr ~= 3;
>
> }
>
> I care about slicing or anything but appending, removal, and indexing. I
> don't even need bounds checking. I don't see a need to get locked in to
> the GC for such simple cases.
>
>

One way is to make x() a function template:

import std.stdio;

void x(int y)() {
    bool[y] arr;
    arr[y/2] = true;
    writeln(arr);
}

void main() {
     x!5();
}

Ali



More information about the Digitalmars-d-learn mailing list