Allocating large 2D array in D

monarch_dodra monarchdodra at gmail.com
Mon Feb 4 22:55:34 PST 2013


On Monday, 4 February 2013 at 22:02:48 UTC, bearophile wrote:
> 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]);
>
> [SNIP]
>
> Bye,
> bearophile

Yeah... but then I do that, I also tend to smash against the 
initial:
//----
struct Arr {
     double[4000][4000] a;
}

Arr* data = new Arr;
writeln(data.a[1]);
//----
"Error: index 4000 overflow for static array"
//----

I don't fully understand what this means, so I haven't complained 
yet. Is this a "legit error" on my end, with the compiler 
protecting me, or a false positive, where the compiler thinks I 
want to allocate Arr on the stack...?


More information about the Digitalmars-d-learn mailing list