Thread overview
Basic dynamic array question. Use of new versus no new.
Jun 11, 2014
WhatMeWorry
Jun 11, 2014
Matt
Jun 11, 2014
Ali Çehreli
Jun 11, 2014
monarch_dodra
Jun 11, 2014
Kapps
Jun 11, 2014
Jonathan M Davis
June 11, 2014
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.
June 11, 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?
June 11, 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.

They both do the same, create an array of n integers on the heap and set the length to n. You can also use .capacity instead of .length to allocate n but not adjust length.
June 11, 2014
On 06/10/2014 08:06 PM, Matt wrote:

> On Wednesday, 11 June 2014 at 02:30:01 UTC, WhatMeWorry wrote:

>> 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

> 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.

Correct but it is not different from the first one, other than doing it in one step.

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

No. TDPL is a D2 book, which has naturally been behind the newer developments since the book has been published. Here is the errata:

  http://erdani.com/tdpl/errata/

Ali

June 11, 2014
On Wednesday, 11 June 2014 at 05:46:07 UTC, Ali Çehreli wrote:
> On 06/10/2014 08:06 PM, Matt wrote:
>
> > On Wednesday, 11 June 2014 at 02:30:01 UTC, WhatMeWorry wrote:
>
> >> 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
>
> > 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.
>
> Correct but it is not different from the first one, other than doing it in one step.

Wait, what? That's not correct. "new int[20]" is simply "one of two" syntaxes to allocate an array of 20 ints, the other being "new int[](20)".

It does NOT allocate a static array, and then slice it. Proof is that there is APPENDABLE data present, which should not happen if you had actually simply allocated a single element of type int[20] (APPENDABLE data is only present for array-style allocation).

AFAIK, there is no "natural" way to allocate either a static array or a slice itslef, using new syntax. It can be done via the "wrapper struct" hack though:

//----
//Used to allocate a slice "int[]".
//Or a static array "int[20]" on the heap.
struct NewWrapper(T)
{
    T data;
}
void main()
{
    int[] arr1 = new int[20];
    int[] arr2 = (new NewWrapper!(int[20])).data[];
    writeln(arr1.length);   //20, as expected
    writeln(arr1.capacity); //31 on my machine
    writeln(arr2.length);   //20
    writeln(arr2.capacity); //0
}
//----

Too bad the ambiguous syntax has us resolve to these tricks. It also prevents value constructing an array of elements. A syntax where the array sizes came *before* the type could have solved these issue:
int size = 20;
new size int[5]([1, 2, 3, 4, 5]);
Allocate "20" (runtime) elements of type "int[5]", each initialized to the value ([1, 2, 3, 4, 5]).

But, well, that ship has set sail long ago :(
June 11, 2014
On Wed, 11 Jun 2014 02:30:00 +0000
WhatMeWorry via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
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.

They do essentially the same thing but, the first one does it in two steps instead of one. To better understand arrays in D, I'd advise reading this article:

http://dlang.org/d-array-article.html

- Jonathan M Davis

June 11, 2014
On Tue, 10 Jun 2014 23:28:16 -0400, Kapps <opantm2+spam@gmail.com> wrote:

> 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.
>
> They both do the same, create an array of n integers on the heap and set the length to n. You can also use .capacity instead of .length to allocate n but not adjust length.

This is slightly incorrect. Here is the difference as I see it:

1. Setting length goes through the extra steps of checking the current length, looking up the existing block (obviously with initial value of null, there isn't one), and allocating if necessary. A lot more code is executed.
2. The 'new' call will ALWAYS allocate a new block, even if array previously had data in it.
3. The block size allocated is not always n, it could be more.
4. You meant 'reserve', not 'capacity'. capacity checks how many elements the block can support.

Which way is easier/better? If you know you are starting with a blank array, then new is the way to go. This avoids any unnecessary extra checks for existing data.

The second way is required if you may already have some data in the array (i.e. prior to setting the length, arr.length != 0).

Also, I would recommend to use:

auto array = new int[20]

To avoid repeating the type.

-Steve