delegates and heap usage

Franciszek Czekala home at valentimex.com
Wed Dec 1 04:36:51 PST 2010


What part of stack frame is exactly copied when a delegate is used? For
example what is the heap usage due to delegates usage in the following code?
The library reference speaks about the enclosing function but I do not think
that it suffices to copy the stack frame of the directly enclosing function.
Is the whole stack copied? What happens in the case of recursive calls?

import std.stdio;

void main(){
  int x = 0;
  alias void delegate () del_t ;

  auto dlg = new del_t[100] ;
  void f(int n){
    if (n<0) return;
    int y = n;
    int[1000] arr;
    void g(){
      x++;
      void h(){
        writeln(y,' ',x);
      }
      dlg[n] = &h;
      writeln("assigned, ", n);
    }
    g();
    f(n-1);
  }
  f(99);
  for(int i = 0; i<100; i++) dlg[i]();
}


More information about the Digitalmars-d mailing list