delegates & GC allocations

hane via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 20 08:21:19 PDT 2014


On Wednesday, 20 August 2014 at 14:44:39 UTC, Etienne wrote:
> I've been hearing that delegates get a context pointer which 
> will be allocated on the GC. Is this also true for delegates 
> which stay in scope?
>
> e.g.
>
> void addThree() {
> 	int val;
> 	void addOne() {
> 		val++;
> 	}
> 	
> 	addOne();
> 	addOne();
> 	addOne();
>
> 	return val;
> }
>
> Will the above function allocate on the GC?

int getInt1() @nogc
{
	int val;
	int func() @nogc { return val; }
	return func();
}

int delegate() getInt2() //@nogc - thie one allocates!
{
	int val;
	int func() @nogc { return val; }
	return &func;
}

int delegate() getInt3() @nogc
{
	int val;
	int func() @nogc { return 0; }
	return &func;
}


More information about the Digitalmars-d-learn mailing list