Basic dynamic array question. Use of new versus no new.

Matt via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 10 20:06:39 PDT 2014


On Wednesday, 11 June 2014 at 02:30:01 UTC, WhatMeWorry wrote:
> In Mr. Cehreli's book it says
>
> Additionally, the length of dynamic arrays can be changed by 
> assigning a value to this property:
>
> int[] array; // initially empty
> array.length = 5; // now has 5 elements
>
> while in Mr. Alexandrescu's book, it says
>
> To create a dynamic array, use a new expression (ยง 2.3.6.1 on 
> page 51) as follows:
>
> int[] array = new int[20]; // Create an array of 20 integers
>
>
> Could someone please compare and contrast the two syntaxes. I 
> presume the "new" command places the 2nd array in heap memory.

I would have read the second as creating a static array of 20 
ints in heap memory, then assigning a dynamic array to point to 
it. The first one definitely does what you'd expect.

Wasn't Andres book based on D1.0? Or has he done another since 
then?


More information about the Digitalmars-d-learn mailing list