Quick int pointer allocation question
Steven Schveighoffer
schveiguy at yahoo.com
Fri Sep 14 13:31:36 PDT 2012
On Fri, 14 Sep 2012 13:03:37 -0400, bearophile <bearophileHUGS at lycos.com>
wrote:
> monarch_dodra:
>
>>> int *p = [5].ptr;
>>>
>>> -Steve
>
> But see this benchmark:
>
>
> void main() {
> auto pointers = new int*[1_000_000];
> foreach (int i, ref p; pointers)
> p = [i].ptr;
> foreach (i; 0U .. 4_000_000_000U) {}
> }
>
>
> On my 32 bit system its RAM commit is about 23 MB. The pointers array
> takes about 4 MB. This means each "int" takes about 19 bytes of heap RAM
> instead of 4. Each int allocates some data (capacity) to extend the
> array.
That has nothing to do with using array literals -- it has to do with the
fact that the minimum heap block is 16-bytes (or 4 ints wide). Extra 3
bytes is probably for overhead and static data.
If instead of p = [i].ptr; you did p = new int; *p = i;
You would get the same exact behavior.
No way around this, unless you want to do custom allocators.
-Steve
More information about the Digitalmars-d-learn
mailing list