Allocating large 2D array in D
Steven Schveighoffer
schveiguy at yahoo.com
Mon Feb 4 08:13:28 PST 2013
On Mon, 04 Feb 2013 10:58:36 -0500, bearophile <bearophileHUGS at lycos.com>
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.
Wow, this is something I didn't know was possible. Very useful!
BTW, "all (but last of) the dimensions" isn't correct, you know them all
in your example, and it looks great!
-Steve
More information about the Digitalmars-d-learn
mailing list