Dynamic arrays

Ali Çehreli via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Tue Sep 1 01:55:07 PDT 2015


On 09/01/2015 12:43 AM, qznc wrote:
> On Monday, 31 August 2015 at 22:03:07 UTC, jmh530 wrote:
>> In general, I find myself very easily getting mixed up by the syntax
>> of the static vs. dynamic arrays. For instance, compare
>> int[] x = new int[3];
>> int[3] y = new int[3];
>> auto z = new int[3];
>> x and y are obviously dynamic and static arrays, respectively.
>> However, my initial guess on z is that is like y, a static array on
>> the heap. But that is wrong. It's actually like x. Easy to get confused.
>
> In general, "new" means heap allocation.
>
> I wonder about the initialization of y. Maybe it creates a temporary
> dynamic array on the heap and then copies it to the static array on the
> stack?

Seems to be so:

import std.stdio;

void main()
{
     int[] x = new int[3];
     int[3] y = new int[3];
     auto z = new int[3];

     writeln(x.ptr);
     writeln(y.ptr);
     writeln(z.ptr);
}

y's elements are on the stack:

7F1375618200
7FFF091A9910
7F1375618220

Ali



More information about the Digitalmars-d-announce mailing list