A question regarding Array capacity, length and slices

Sourav via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 29 00:37:16 PDT 2014


Hello,

I am quite new to D and working my way through the D tutorials. 
While learning about slices and capacity I found that depending 
upon the initial number of elements in a slice (and hence its 
capacity) the behavior of the code changes. Consider this program:

<snip>
import std.stdio;

void main()
{
     int[] original = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14];
     //int[] original = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15];
     int[] duplicate = original[0..$-1];

     writefln("original = %s", original);
     writefln("duplicate = %s", duplicate);

     writefln("original(capacity = %s, length = %s)", 
original.capacity, original.length);
     writefln("duplicate(capacity = %s, length = %s)", 
duplicate.capacity, duplicate.length);

     original ~= 100;
     duplicate[2] = 200;

     writeln("\nAFTER\n");

     writefln("original = %s", original);
     writefln("duplicate = %s", duplicate);

     writefln("original(capacity = %s, length = %s)", 
original.capacity, original.length);
     writefln("duplicate(capacity = %s, length = %s)", 
duplicate.capacity, duplicate.length);
}

</snip>

The output of this program is:

original = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
duplicate = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
original(capacity = 15, length = 14)
duplicate(capacity = 0, length = 13)

AFTER

original = [1, 2, 200, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 100]
duplicate = [1, 2, 200, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
original(capacity = 15, length = 15)
duplicate(capacity = 0, length = 13)

However, if the original slice in the comment is used instead of 
the first one:

<snip>
int[] original = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15];
</snip>

the output changes to:

original = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
duplicate = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
original(capacity = 15, length = 15)
duplicate(capacity = 0, length = 14)

AFTER

original = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
100]
duplicate = [1, 2, 200, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
original(capacity = 31, length = 16)
duplicate(capacity = 0, length = 14)

I understand that in the first case, capacity was one more than 
length and so there was space to allocate one more element. So 
the slice was not reallocated to a different space and 
original/duplicate referred to the same array (and both had third 
element updated to 200). But in the second case original's 
capacity was full and it was reallocated and hence update to 
duplicate was not reflected in original. But what looks strange 
to me is that the behavior totally depends upon how many elements 
were present initially in the array, which means, the capacity of 
a slice can actually introduces little surprises in the code! Is 
this expected behavior or am I entirely missing something here?

Thanks!


More information about the Digitalmars-d-learn mailing list