Found this unanswered question on StackOverflow.

This program:

import std.stdio;

void add(ref int[] data)
{
    data ~= 1;
    data ~= 2;
}

void main()
{
    int[] a;
    writeln("capacity:",a.capacity);
auto cap = a.reserve(1000); // allocated may be more than requested
    assert(cap >= 1000);
    assert(cap == a.capacity);
    writeln("capacity:",a.capacity);
    a.add();
    writeln(a);

}

compiled with "dmd -profile=gc"

has this output in profilegc.log

bytes allocated, allocations, type, function, file:line
              4               1 int[] profiling.add profiling.d:8
              4               1 int[] profiling.add profiling.d:7

The question is: why doesn't using reserve() cause an allocation to be shown?

Reply via email to