char[] == null

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 19 00:30:44 PST 2015


On Wednesday, November 18, 2015 22:15:19 anonymous via Digitalmars-d-learn wrote:
> On 18.11.2015 22:02, rsw0x wrote:
> > slices aren't arrays
> > http://dlang.org/d-array-article.html
>
> The language reference/specification [1] uses the term "dynamic array"
> for T[] types. Let's not enforce a slang that's different from that.
>
>
> [1] http://dlang.org/arrays.html

Exactly. T[] _is_ a dynamic array. Steven's otherwise wonderful article on
arrays in D ( http://dlang.org/d-array-article.html ) made the mistake of
calling the buffer that T[] points to on the GC heap (assuming that even
does point to the GC heap) the dynamic array. And per the language spec,
that's not true at all.

T[] is a dynamic array, whereas T[n] is a static array. The buffer referred
to by T[] has no official name. And actually, the dynamic array really
doesn't care whether it refers to a buffer on the GC heap, on that was
malloced, a slice of a static array, etc. All of its operations are the same
regardless, and all of the work. It's just that if the dynamic array does
not refer to a buffer allocated on the GC heap for dynamic arrays, then it
doesn't have any excess capacity for the dynamic array to grow into when you
append to it, so appending to it forces the GC to reallocate its memory (as
opposed to only having to reallocate sometimes when the buffer is already
GC-allocated). And since the dynamic array itself does not manage its own
memory, if you slice something other than GC-allocated memory to get a
dynamic array, then it's up to you to make sure that you don't leak that
memory or refer to it after it's been freed. However, every other aspect of
T[] is identical regardless of what memory backs it, and most code really
doesn't care what memory backs it.

As far as the D language is concern, T[] _is_ a slice of memory, but it's
also a dynamic array, whereas the memory that it's slicing is _not_ a
dynamic array.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list