Void initialization

bearophile bearophileHUGS at lycos.com
Mon Dec 19 04:12:49 PST 2011


> Using D1, I have a program that creates tons of float[] ; for performance
> reasons, I would like them to be uninitialized.
> I've tried replacing
> float[] f = new float[x];
> by
> float[] f = cast(float[])std.gc.malloc(x*4);

Try something like this (untested):

alias float TF;
TF[] f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x];

Using x*4 is error-prone. I suggest to wrap that code inside a templated function, where T is the type of the items, to avoid some bugs.


More information about the Digitalmars-d-learn mailing list