Allocating large 2D array in D

bearophile bearophileHUGS at lycos.com
Mon Feb 4 07:58:36 PST 2013


monarch_dodra:

> If all (but last of) the dimensions are known at compile time, 
> then you can dynamically allocate an array of fixed sized 
> arrays:
>
> //----
> enum size_t gridSize = 4_000;
> enum size_t total   = gridSize * gridSize;
> static assert (total == 16_000_000); //16 million doubles total
> static assert (total * double.sizeof == 128_000_000); //126 
> Megs allocated
>
> void main()
> {
>     double[gridSize][] gridInfo = new 
> double[gridSize][](gridSize);
> }
> //----
>
> This will give you a dense array: Eg: all the data is 
> contiguous in memory.

Nice. This idiom should be added to the D docs.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list