Allocating large 2D array in D

bearophile bearophileHUGS at lycos.com
Mon Feb 4 07:28:57 PST 2013


Sparsh Mittal:

> I am allocating 2d array as:
>
> double[gridSize][gridSize] gridInfo;
>
> which works for small dimension, but for large dimension, 16Mb 
> limit comes.

Walter has decided to introduce a very low limit for the size of 
static arrays. (Both Clang and G++ support larger ones).


> Would you please tell me how do allocate a large 2d array 
> (which has to be done as a dynamic array)? It is a square grid 
> and dimensions are already known.

This allocates your array, filled with NaNs:

auto gridInfo = new double[][](gridSize, gridSize);


If you want to skip most or all the initialization then take a 
look at two functions in std.array. In most cases the "partially 
initialized" function is enough, and it's quite safer.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list