Dynamic array ot not

forkit forkit at gmail.com
Sun Jan 16 23:14:43 UTC 2022


On Sunday, 16 January 2022 at 23:03:49 UTC, Ali Çehreli wrote:
>
> That's not correct. There are many range algorithms that are 
> lazy to defer memory allocation but array() is not one of 
> those. array() does eagerly allocate memory, which is it's 
> whole purpose:
>
>   https://dlang.org/phobos/std_array.html#array
>
> "Allocates an array and initializes it with copies of the 
> elements of range r."
>
> So, that range expression has the same allocations as your "GC 
> allocation" line above.
>

Honestly, that is what I thought, and expected.

However, when I compiled with: -profile=gc  .. it indicated no GC 
allocation going on. That's really why I got confused - because, 
as you say, the array statement (as I understand it)will result 
in a dynamically allocated array (i.e. allocated on GC heap).

But -profile=gc .. says no. It's not.

int[][] mArr = [[1, 2], [3, 4], [5, 6], [7, 8]]; // GC allocation 
occurs.
int[][] mArr = iota(1, 9).chunks(2).map!array.array; // No GC 
allocation occurs.



More information about the Digitalmars-d-learn mailing list