length = 0 clears reserve

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 10 18:59:57 PDT 2017


On Tuesday, April 11, 2017 01:42:32 Jethro via Digitalmars-d-learn wrote:
> arrays have the ability to reserve but when setting the length to
> 0, it removes the reserve!! ;/
>
> char[] buf;
> buf.reserve = 1000;
> buf.length = 0;
> assert(buf.capacity == 0);
>
> But I simply want to clear the buffer, not change it's
> reserve/capacity.
>
> I've tried to hack by setting the length to 0 through a pointer,
> but that still clears the capacity!
>
> I want to do this because I want to be able to reuse the array
> without ever reallocating(I'll set the capacity to the max that
> will ever be used, I don't have to worry about conflicts since it
> will always be ran serially).
>
> It is a bug if dmd is setting the capacity to 0 and reallocating
> the array when the length is set to 0. It should either do it
> lazily or not at all(or, say setting the length to -1 does not
> clear the capacity, only sets the length to 0).

You can't reuse the memory of a dynamic array by simply setting its length
to 0. If that were allowed, it would risk allow dynamic arrays to stomp on
each others memory (since there is no guarantee that there are no other
dynamic arrays referring to the same memory). However, if you know that
there are no other dynamic arrays referrin to the same memory, then you can
call assumeSafeAppend on the dynamic array, and then the runtime will assume
that there are no other dynamic arrays referring to the same memory.

If you haven't already, I would advise reading

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

It uses the wrong terminology, but otherwise, it's an excellent, informative
article. The article refers to T[] as a slice, and the GC-allocated block of
memory as a dynamic array. However, the official terminology is that T[] is
a dynamic array which refers to a block of memory which may or may not be
GC-allocated, and there is no special term for a GC-allocated block of
memory. T[] _is_ also a slice of whatever memory it refers to, but it's the
dynamic array, not the block of memory. And in fact, all of the dynamic
array operations work perfectly fine regardless of whether the block of
memory is GC-allocated or not (it's just that if it's not, the capacity is
0, so appending will always result in a reallocation). In any case, while
the terminology in the article isn't quite right, it gives a good overview
of how dynamic arrays work in D, and it explains what's going with the
capacity of the array and why setting the length of a dynamic array to 0
results in the capacity being 0.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list