Void initialization

Steven Schveighoffer schveiguy at yahoo.com
Mon Dec 19 08:32:24 PST 2011

On Mon, 19 Dec 2011 07:04:20 -0500, Bear <joanyleprince at yahoo.fr> wrote:

> 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);

this is wrong.  a float[] slice is actually a struct, whereas gc.malloc  
returns a pointer.

What you have done is cast a pointer into a pointer+length struct, leaving  
anyones guess as to what the length is set to.  I don't even know why this  

A slice is a pointer + length, and you can slice a pointer to "add a  
length" to it.  Follow bearophile's suggestion.

> However
> f[] = float.nan;
> solved the problem, but kinda defeats the purpose of using malloc...
> What am I doing wrong?

If this works, it's not doing what you think :)


More information about the Digitalmars-d-learn mailing list