Setting array length to 0 discards reserved allocation?
Andrew Godfrey via Digitalmars-d
digitalmars-d at puremagic.com
Sat Jul 26 16:06:00 PDT 2014
On Friday, 25 July 2014 at 15:55:50 UTC, H. S. Teoh via
Digitalmars-d wrote:
> On Fri, Jul 25, 2014 at 03:07:53PM +0000, Andrew Godfrey via
> Digitalmars-d wrote:
>> On Friday, 25 July 2014 at 04:38:40 UTC, Jonathan M Davis via
>> Digitalmars-d
>> wrote:
>> >You really should read this article:
>> >
>> >http://dlang.org/d-array-article.html
>>
>> Thank you, that was educational.
>> What I needed to read was that "A slice does not own the
>> array, it
>> references the array." - which I knew, but I hadn't
>> internalized that
>> even when a dynamic array is a private member, it is accessed
>> via a
>> slice and the runtime is not aware when there's a 1-to-1
>> correspondence between the two.
>>
>> The documentation could help a bit more. I'm game to
>> try to make a pull request for this, but I'm wondering
>> if the library docs can actually point to the article?
>
> That's a very good idea. I think that would dispel a lot of
> misconceptions that newcomers might have.
>
>
>> More feasibly, I'm thinking:
>> Both the documentation for length() (http://dlang.org/arrays)
>> and reserve() (http://dlang.org/phobos/std_array.html)
>> should at least mention assumeSafeAppend.
>> An example similar to what I posted may also be worthwhile.
> [...]
>
> Yes, please submit a PR for this.
So I've been looking into this, and I've realized the 'arrays'
page is actually
inconsistent. (http://dlang.org/arrays).
Instead of spending time on a big update, I'll outline a proposal
here.
This page generally uses the term "array" to mean the slice, and
"array data"
to mean the allocated data that a slice currently refers to.
(In contrast, the handy article calls these "slice" and "dynamic
array" respectively).
For example:
> Dynamic Arrays
> int[] a;
> Dynamic arrays consist of a length and a pointer to the array
> data. Multiple > dynamic arrays can share all or parts of the
> array data.
Problems start early on:
> Slicing
> Slicing an array means to specify a subarray of it. An array
> slice does not
> copy the data, it is only another reference to it.
By this page's terminology, this should actually say something
like this:
"slicing an array means to specify a subarray of that array's
current array data". e.g. Causing the first 'array' (slice) to
grow would dissociate the slices from each other.
Because the term "slice" is so foreign,
I sympathize with how the term was avoided to start with. But the
fact
is that a dynamic array declaration creates two separate things,
and so
I'd propose to give them reliable names at the earliest.
So: Immediately after the section that introduces dynamic arrays,
I would
then put the section on slices, and rather than describing a
slice as a "subarray", I would introduce it as an important part
of the workings
of a dynamic array. So I'm thinking the first example of a slice
should be
a simplification of the article's example:
int[] a = new int[5]; // A new dynamic array, with slice 'a'
pointing to it.
int[] b = a[]; // We now have two slices pointing to
one dynamic array.
a[1] = 42;
assert(b[1] == 42); // Demonstrating the point
Thereafter can come sub-slice examples and so on.
Does this make sense?
More information about the Digitalmars-d
mailing list