Setting array length to 0 discards reserved allocation?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 27 06:54:13 PDT 2014


On Sunday, 27 July 2014 at 13:03:13 UTC, Jakob Ovrum wrote:
> On Sunday, 27 July 2014 at 12:03:05 UTC, Jonathan M Davis wrote:
>> Except that no concepts are being conflated. T[] is the 
>> dynamic array. There's a block of memory managed by the GC 
>> underneath, but it's completely hidden from the programmer. It 
>> is _not_ the dynamic array. It's just a block of memory 
>> managed by the GC which is used to manage the memory for 
>> dynamic arrays, and it has a completely different type from 
>> T[]. It's not even an array in the D sense. The block of 
>> memory is referred to by a pointer, not a D array.
>>
>> It's talk of "which array" owns the memory and the like which 
>> causes confusion, and talking about the GC-managed block of 
>> memory as being the dynamic array and the T[] that the 
>> programmer sees as being the slice is just plain wrong. The 
>> T[] is both a slice and a dynamic array (because they're the 
>> same thing), whereas the block of memory is neither. I think 
>> that the fact that the article tried to call the underlying 
>> block of memory a dynamic array has caused a lot of 
>> unnecessary confusion.
>>
>> - Jonathan M Davis
>
> This very thread is a testament to the fact that no, it's not 
> completely hidden, and it is the conflation that causes 
> confusion, and clarity only comes when the two concepts are 
> explained separately.
>
> This matches my experience when I teach about D's arrays on IRC 
> too - people generally feel enlightened after reading the array 
> article.
>
> It's debatable whether hamfisting dynamic array capabilities 
> onto a simple slice type was a good idea or not (T[new], 
> anyone?), but we should be as clear with the current situation 
> as possible. Pretending there isn't a problem is ignoring the 
> cries of D newbies everywhere. Please stop.

The array article does a great job explaining a lot about D 
arrays, and it definitely has helped people understand them. But 
it uses the wrong terminology, and that's my point. In D, T[] is 
a dynamic array and a slice. As far as D is concerned, there is 
no difference. T[] normally refers to a block of memory that the 
GC manages, but that block of memory is not typed as a dynamic 
array. And that's where the article gets it wrong.

- Jonathan M Davis


More information about the Digitalmars-d mailing list