Setting array length to 0 discards reserved allocation?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 24 21:38:15 PDT 2014


On Thu, 24 Jul 2014 23:30:11 +0000
Andrew Godfrey via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> Is this a bug? Or am I misunderstanding
> something?

It's not a bug. If it did it any other way, you'd have problems with arrays
stomping on each others memory. The underlying data structure keeps track of
the farthest point in the block which an array has been expanded to, but it
doesn't keep track of what arrays exist that point to it, so it can't know
which ones point where. So, it has no way of knowing that the slice whose
length you're setting to 0 is the only slice. assumeSafeAppend is used to fix
that problem, but you have to be sure that it _is_ safe to append to that
array without stomping on other arrays, or you're going to have problems.

You really should read this article:

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

- Jonathan M Davis


More information about the Digitalmars-d mailing list