Slices and Dynamic Arrays

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Dec 29 23:13:20 UTC 2017


On Friday, December 29, 2017 22:32:01 Tony via Digitalmars-d-learn wrote:
> In DLang Tour:Arrays
> https://tour.dlang.org/tour/en/basics/arrays
>
> there is:
> -----------------------------------------------
> int size = 8; // run-time variable
> int[] arr = new int[size];
>
> The type of arr is int[], which is a slice.
> -----------------------------------------------
>
> In "D Slices"
> https://dlang.org/d-array-article.html
>
> there is:
> -----------------------------------------------
> int[] a;             // a is a slice
> ------------------------------------------------
>
> Based on those two web pages it appears that the name for a
> dynamic array <reference?> in D is "slice". That is, anytime you
> have a dynamic array (even a null reference version) it is called
> a slice. Is that correct?

No. The term "slice" is a bit overused in D, meaning a variety of things. It
doesn't help that some folks dislike the official terminology. In general, a
slice is a contiguous group of elements. A slice of memory would be a
contiguous block of memory. A dynamic array therefore refers to a slice of
memory and could be called a slice, but it's also the case that using the
slice operater on a container is called slicing - e.g. rbt[] would give you
a range over the container rbt, and that range is a slice of the container,
but it's not an array at all.

The "D Slices" article is great for explaining how things work but does not
use the official terminology. Per the official terminology, T[] is a dynamic
array regardless of what memory it refers to. Assuming that it's non-null,
it _does_ refer to a slice of memory, and a number of folks follow the
article and call T[] a slice and refer to the GC-allocated memory that a T[]
usually refers to as the dynamic array, but officially, T[] is the dynamic
array, and the memory it refers to has no special name. It's just a block of
memory that is usually GC-allocated, but it could be malloced or be a slice
of a static array or any other type of memory (since you can create a
dynamic array from pointers), and its semantics don't change based on what
type of memory backs it.

I gather that the author chose to refer to the GC-allocated block of memory
as the dynamic array because of how the term dynamic array is sometimes used
elsewhere in computer science, but as far as D is concerned, T[] is a
dynamic array regardless of what memory it refers to, and personally, I
think that focusing on the GC-allocated memory block as being the dynamic
array causes confusion when dealing with dynamic arrays that refer to
non-GC-allocated memory, since the semantics are identical regardless of
what memory backs the array (it's just that if the array does not refer to a
slice of GC-allocated memory, the capacity is always 0, guaranteed that
appending will reallocate instead of it just being a possibility), but too
many folks tend to think of them as being different. A dynamic array that
refers to non-GC-allocated memory is really the same thing as one that is
GC-allocated except that when you slice non-GC-allocated memory to create a
dynamic array, you then need to be aware of how to manage the lifetime of
that memory so that no dynamic array refering to it outlives it (just like
you'd have to do with a pointer to non-GC-allocated memory). But dynamic
arrays never manage their own memory. It's just that the GC does the
managing by default and will reallocate the memory backing a dynamic array
if an operation can't be done in-place.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list