Function's local variables allocated on the heap.

Ali Çehreli acehreli at yahoo.com
Sun Aug 31 22:11:26 UTC 2025


On 8/31/25 2:36 PM, realhet wrote:
 > Hi,
 >
 > I noticed that the local area of the myUi() function was allocated on
 > the heap, not on the stack as it would be the usual case with functions.

Unless that's an optimization, that part would be the same: on the 
stack. However, because the delegate necessitates long-term lifetime, 
its context has to be on GC memory.

So, either the compiler allocates on the heap and uses that for local 
stack frame, or allocates on the stack and then copies to heap. I think 
the former is not complicated. I am guessing...

 > Please explain me how the D compiler detects that. Does it knows that
 > the myUi() function returns something, that contains a delegate which is
 > referencing to a variable on the function's local area?

Yes.

Ali



More information about the Digitalmars-d-learn mailing list