Full closures for D

Extrawurst spam at extrawurst.org
Fri Nov 2 16:57:40 PDT 2007


Jarrett Billingsley schrieb:
> "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.) 
>   

Prints:
13FF28
13FF2C

...whatever that means...


~Extrawurst
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20071103/5362e56a/attachment.html>


More information about the Digitalmars-d mailing list