Full closures for D
Jarrett Billingsley
kb3ctd2 at yahoo.com
Fri Nov 2 16:24:43 PDT 2007
"Xinok" <xnknet at gmail.com> wrote in message
news:fgg9q8$jlr$1 at digitalmars.com...
> It seems that variables that are used by a nested function are allocated
> on the heap rather than the stack. This was my test code:
>
> void delegate() foo(){
> int v = 60;
> int c = 35;
> writefln(&c);
> writefln(&v);
> return {writefln(&v);};
> }
>
> void main(){
> void delegate() one = foo();
> one();
> }
>
> Prints:
> 12FF18
> 8B2FF4
> 8B2FF4
>
> The address of 'v' doesn't change. What you do notice is the great
> difference of the memory addresses between int v and int c, which suggests
> that 'v' is allocated on the heap rather than the stack.
>
Hm. Don't have a D2 compiler with me -- could you run the following and
tell me what it prints?
void main()
{
int v, c;
void foo()
{
writefln(&c);
}
writefln(&v);
writefln(&c);
}
I'm wondering if the compiler is smart enough not to allocate variables on
the heap if it doesn't have to. (I'm not returning foo.)
More information about the Digitalmars-d
mailing list