Unitialized allocation

Jarrett Billingsley kb3ctd2 at yahoo.com
Fri Jun 27 07:10:43 PDT 2008


"Mael" <mael.primet at gmail.com> wrote in message 
news:g42p4r$2air$1 at digitalmars.com...
> Hello,
>
> another newbie question :
> I think it is possible to allocate unitialized data on the stack using 
> char[512] mydata = void ;
> is there a way to allocate unitialized data on the heap ?

With Phobos, std.gc.malloc is supposed to return uninitialized data.  Here's 
a little template function to make it easy:

import std.gc;

T[] allocUninit(T)(size_t len)
{
    return (cast(T*)std.gc.malloc(len * T.sizeof))[0 .. len];
}

...

auto arr = allocUninit!(char)(512);

The equivalent in Tango is GC.malloc from tango.core.Memory. 




More information about the Digitalmars-d-learn mailing list