Allocating large 2D array in D

bearophile bearophileHUGS at lycos.com
Mon Feb 4 14:02:47 PST 2013


monarch_dodra:

> Ideally, I wish we could allocate static arrays on the heap 
> easily:
>
> "int[2]* p = new int[2]()"

To do that I wrap the array inside a static struct:

struct Arr {
     int[2] a;
}

Arr* data = new Arr;
writeln(data.a[1]);


> Anybody know why this doesn't work?

Maybe it's just a matter of syntax. You see it if you use:

struct Arr {
     int[2] a;
     alias this = a;
}
Arr* data = new Arr;

Now what's data[1]? It's data.a[1] or is it the second (missing) 
struct Arr?

So to do that you need transparent references, like "ref int[2]", 
that in D are only present inside functions.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list