new T[size] vs .reserve

FG home at fgda.pl
Sat Feb 2 09:48:17 PST 2013


On 2013-02-02 17:36, Namespace wrote:
>      ubyte[] arr = new ubyte[4];
>      arr ~= 4; // desirable: [4, 0, 0, 0];

Why not arr[0] = 4? What is so special about having 4 elements?

> As I looked at arr.length and arr.capacity for the first time I was schocked: I
> want only space for 4 items, but I got space for 15.

I think you are out of luck with built-in dynamic arrays.
It seems that even for an array of length 1 it gives a minimum of 15 bytes
storage, so you can fit 15 bytes, 7 shorts, 3 ints, 1 long.
I'm not sure why is it 15, given alignment an all...


More information about the Digitalmars-d-learn mailing list