runtime static arrays

Jonathan M Davis jmdavisProg at gmx.com
Tue Nov 20 14:59:34 PST 2012


On Tuesday, November 20, 2012 14:52:56 Ali Çehreli wrote:
> On 11/20/2012 02:46 PM, Namespace wrote:
> > Something like:
> > scope int[8] arr;
> > or
> > scope int[i] arr;
> > 
> > would be cool. You get an resizeable array which capactiy is all the
> > time equal to his length. And it is destroyed after the liftetime of the
> > scope.
> > 
> > Maybe some stuff for Remus...
> 
> This is a surprisingly promising start: :)
> 
> import std.stdio;
> 
> struct FixedArray(T, size_t N)
> {
>      T[N] elements;
>      alias elements this;
> }
> 
> void main()
> {
>      FixedArray!(int, 10) a;
> 
>      foreach (int i, ref e; a) {
>          e = 42 + i;
>      }
> 
>      writeln(a);
> }

How is that any different from just using a static array? All you've done is 
wrap it. You still can't set its size at runtime. Isn't what the OP is 
essentially looking for is a static array whose length can be set at runtime? 
This doesn't solve that. For that, you need something which is going to 
allocate on the heap at runtime but deterministically free that that memory 
when it's done.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list