new T[size] vs .reserve

FG home at fgda.pl
Sat Feb 2 13:57:58 PST 2013


On 2013-02-02 22:33, Steven Schveighoffer wrote:
> Heap block sizes start at 16.  One byte overhead is used to store the array
> length, so the minimum size of ANY array allocation is 15 bytes.

How is the length stored because I see only strange numbers in that byte.

     foreach (end; 2..31) {
         ubyte[] x;
         foreach (i; 0..end) {
             x ~= cast(ubyte)i;
         }
         writeln(x.length, " ", x.capacity, " ", x.ptr[end]);
     }

That code outputs following length/capacity/extra_byte:

2 15 69
3 15 0
4 15 100
5 15 26
6 15 36
7 15 0
8 15 0
9 15 0
10 15 0
11 15 0
12 15 0
13 15 0
14 15 0
15 15 15
16 31 0
17 31 0
18 31 0
19 31 0
20 31 0
21 31 0
22 31 0
23 31 0
24 31 0
25 31 0
26 31 0
27 31 0
28 31 0
29 31 0
30 31 0

Last byte's value for length == 2 is quite random on each execution.
In a smaller way also for length == 6. Other values seem constant.



More information about the Digitalmars-d-learn mailing list