Dynamic array and slices (need Walter and Andrei decision)

Jonathan M Davis jmdavisProg at gmx.com
Thu May 30 12:36:37 PDT 2013


On Thursday, May 30, 2013 21:15:19 Brad Anderson wrote:
> On Thursday, 30 May 2013 at 18:46:17 UTC, deadalnix wrote:
> > An array IS a slice. A slice IS an array. Nothing contradict
> > anything.
> 
> There is some...peculiar stuff about slices though. If you slice
> a static array or a pointer and append to it the slice then
> points to a new distinct GC allocated array. It's not all that
> different from appending to a slice backed by a dynamic array an
> exhausted capacity but it can be surprising to people who don't
> know about this.

But that really doesn't have anything to do with the terminology used. When 
you operate on T[], you don't know what it refers to, and for the most part, 
you don't care (there are of course some exceptions with regards to escaping 
slices of static arrays and the like, but in general, what actually owns the 
memory for T[] is irrelevant).

The problem is that the spec refers to T[] as dynamic arrays as well as 
slices, and the article takes the point of view that T[] isn't a dynamic array 
at all but just a slice of one with the dynamic array being the memory owned 
by the GC. It was written that way because Steven didn't like how the term 
dynamic array as used by the spec does not fully correspond to the general 
computer science term (since in CS in general, dynamic arrays own their 
memory, but they don't in D - the GC does).

And so we have a disagreement as to whether T[] should be referred to as a 
dynamic array (in which case array slices and dynamic arrays are exactly the 
same thing), or whether the GC-own memory that T[] refers to should be 
referred to as the dynamic array.

> Perhaps appending to a slice of non-gc allocated memory should be
> an error (you can't append to a static array, for instance).

Why? It would have to be runtime error, not a compilation error, since the 
compiler can't possible detect it, and it can be quite useful to be able to 
pass a slice of a static array to a function which operates on dynamic arrays, 
and if it appends to it to create a new array, that's fine, because it's 
exactly the same thing that happens when a dynamic array has no extra capacity 
and you try to append to it. Trying to make appending to a slice of a static 
arary an error would just create an inconsistency in the language. Array 
slices aren't designed to know or care who owns the memory that they refer to. 
The only time when it matters is when you append to them, in which case, if 
they have no extra capacity (and not being allocated by the GC always means 
that they don't), then a new array is allocated on the GC heap. If you want to 
avoid that allocation, just avoid appending to arrays. You risk the allocation 
with slices which refer to GC memory as you do with any other kind of array 
slice.

- Jonathan M Davis


More information about the Digitalmars-d mailing list