This language bar is your friend. Select your favorite languages!

Idiom #207 allocate a list that is automatically deallocated

Allocate a list / array a containing n elements (n assumed to be too large for a stack) that is automatically deallocated when the program exits the scope it is declared in.

var
  a: array of some_type;
...
  SetLength(a, n);
...
  integer, dimension(:), allocatable :: a
  allocate (a(n))
{
    my @a = (undef) x $n;
    # scope ends at closing brace
} 

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

tkoenig