Setting array length to 0 discards reserved allocation?

Andrew Godfrey via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 29 17:41:42 PDT 2014


On Tuesday, 29 July 2014 at 20:06:28 UTC, Marc Schütz wrote:
>>    int[3] a = [1, 0, -1];
>>    int[] b = a;
>>    int[] c = new int[4];
>>
>>    b[] = c[1..4];
>>    assert(a[2] == 0);
>>
>>    // This copies a slice of the slice c onto the slice b 
>> (which refers to a).
>
> Playing the devil's advocate:
>
> Yes, it does so by assigning a slice from `c` to a slice from 
> `b`.

Let's try to explain why this:
     b = c[1..4];
changes what b refers to, while this:
     b[] = c[1..4];
copies.

Well, if you assign a slice to a slice, that means 'copy 
elements'.
But if you assign a slice to <what b is>, that means change the 
reference.


More information about the Digitalmars-d mailing list