On 04/29/2014 10:01 AM, Meta wrote:

> On Tuesday, 29 April 2014 at 16:52:27 UTC, Ali Çehreli wrote:
>> That may be misleading because there is no need to allocate with an
>> explicit new. For example, the slice below is owned by the GC as well:
>>
>> int[] foo()
>> {
>>     int[] a;

a is an empty slice, ready for use.

>>     a ~= 42;    // on memory owned by the GC

Since there is no room for the new element, an area large enough for the new element and for some more is allocated from the GC. Now a is a handle to that one element.

Adding the following line reveals that the memory that has just been allocate has room for more elements:

    writeln(a.capacity);    // printed 3 for me

>>     return a;
>> }

Yes, a's life ends but the slice that is being returned from the function is still alive. So, the GC does not free the memory yet.

Ali

Reply via email to