Syntax for heap allocated void initialized arrays

bearophile bearophileHUGS at lycos.com
Sat Sep 21 03:40:05 PDT 2013


simendsjo:

> This is incorrect, but what is the correct syntax? The arrays 
> page only says it's "an advanced feature", but doesn't show the 
> syntax.
>
> int[] a = new int[1](void);

The simplest way to allocate a void-initialized GC-managed 
dynamic array in D is probably to use one of the two functions 
designed for such purpose inside std.array. I suggest to use 
minimallyInitializedArray and to avoid uninitializedArray unless 
you have very special needs. minimallyInitializedArray is 
GC-safer.

auto a = minimallyInitializedArray!(int[])(1);

If you don't want a dynamic array, but a static one, then the 
simplest solution is to wrap it with a static struct and allocate 
it using a GC.malloc...

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list