GC allocation

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 21 10:27:09 PDT 2016


On Thursday, 21 April 2016 at 15:44:56 UTC, QAston wrote:
>
> Closure (delegate type) objects have to allocate because 
> they're reference types and have state. For stateful reference 
> types to be safe they have to be put on the GC allocated heap.

Ok. So, does this mean, that they just allocate on 
creation/binding them? If so, there is no problem and there are 
no questions any more.

>
> In other words - closures work just like classes. The 
> allocation is done for each instance of closure you create and 
> stores the variables you captured from the stack. When you 
> don't capture you can use plain functions which don't use GC.

I have an unusual caption... On creation I capture an immutable 
id for my delegates, which are stored in an array. Indeed, the id 
of the delegate is just the position of it in its array.
Then, I call my delegate as a function with a parameter, 
depending on it, the call is delegated to another objects (with 
the stored id, of course :) )
Another possibility, which I could imagine is: not to store the 
id and let the delegate calculate it by some pointer arithmetic 
from the array wherein it is stored. Then, no independent data 
would be stored at all. This would assume however, that different 
function pointers won't be merged, although the one and only 
distinction between them would be the fact of storing them at 
different array indices.

> See also:
> https://dlang.org/spec/function.html#closures

yeah... have seen the link already, thanks!


More information about the Digitalmars-d-learn mailing list