Allocating large 2D array in D

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Feb 5 09:42:31 PST 2013


On Mon, Feb 04, 2013 at 04:58:36PM +0100, bearophile wrote:
> 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.
[...]

Added to wiki: http://wiki.dlang.org/Dense_multidimensional_arrays


T

-- 
If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell


More information about the Digitalmars-d-learn mailing list