Linus with some good observations on garbage collection

Iain Buclaw ibuclaw at ubuntu.com
Sat Apr 23 03:54:33 PDT 2011


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> Iain Buclaw:
> > Variable length arrays are just sugary syntax for a call to alloca.
> I have an enhancement request in Bugzilla on VLA, with longer discussions. Just
two comments:
> - It seems alloca() can be implemented with two different semantics: to
deallocate at the end of the function or to deallocate at the end of the scope.
Usually alloca() deallocates at the end of the function, but that semantic
confusion is dangerous. VLA deallocate at the end of the scope, just like any
other static array.

Yep, my understanding of the way C99 goes about it (I might be wrong here, so
don't hurt me :), the stack is saved for the lifetime of the alloca'd array, and
restored once it goes out of scope.

ie:

int sz = 42;
{
  void * saved_stack = __stack_save (); // start of scope
  int array[0 : D.range];
  uint D.range;  // automatic variable
  void * D.ptr;  // automatic variable

  D.range = sz + -1;
  D.ptr = __builtin_alloca (sz * 4);
  array = (int[0 : D.range] *) D.ptr;

  // later...

  __stack_restore (saved_stack);        // end of scope
}


More information about the Digitalmars-d mailing list