new T[size] vs .reserve
FG
home at fgda.pl
Sat Feb 2 14:38:35 PST 2013
On 2013-02-02 22:57, FG wrote:
> 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]);
> }
>
Ah, sorry. Silly me.
I have figured out that the length byte would have to be aligned to the end
of the block, so that is where I should look. The updated code:
// size_t ends = [1,2,7,15,31]; // crashes DMD
foreach (end; 2..31) {
ubyte[] x;
foreach (i; 0..end) {
x ~= cast(ubyte)i;
}
writeln(x.length, " ", x.capacity, " ", x.ptr[x.capacity]);
}
return;
shows that indeed, there's length written at the end. :)
Output:
2 15 2
3 15 3
4 15 4
5 15 5
6 15 6
7 15 7
8 15 8
9 15 9
10 15 10
11 15 11
12 15 12
13 15 13
14 15 14
15 15 15
16 31 16
17 31 17
18 31 18
19 31 19
20 31 20
21 31 21
22 31 22
23 31 23
24 31 24
25 31 25
26 31 26
27 31 27
28 31 28
29 31 29
30 31 30
More information about the Digitalmars-d-learn
mailing list