digitalmars.D.learn - Unitialized allocation
- Mael (4/4) Jun 27 2008 Hello,
- Jarrett Billingsley (12/17) Jun 27 2008 With Phobos, std.gc.malloc is supposed to return uninitialized data. He...
- Koroskin Denis (3/22) Jun 27 2008 This surely should be available at a language level.
- Jarrett Billingsley (5/6) Jun 27 2008 It would be nice, but I'm not sure how it'd fit into the syntax.
- Steven Schveighoffer (10/16) Jun 27 2008 New can take parameters, possibly one overload could be an enum that
- Koroskin Denis (31/38) Jun 27 2008 Think different!
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 ?
Jun 27 2008
"Mael" <mael.primet gmail.com> wrote in message news:g42p4r$2air$1 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.
Jun 27 2008
On Fri, 27 Jun 2008 18:10:43 +0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:"Mael" <mael.primet gmail.com> wrote in message news:g42p4r$2air$1 digitalmars.com...This surely should be available at a language level.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.
Jun 27 2008
"Koroskin Denis" <2korden gmail.com> wrote in message news:op.udevrrqpenyajd proton.creatstudio.intranet...This surely should be available at a language level.It would be nice, but I'm not sure how it'd fit into the syntax. new(void) char[512] ? new void char[512] ?
Jun 27 2008
"Jarrett Billingsley" wrote"Koroskin Denis" wroteNew can take parameters, possibly one overload could be an enum that signifies how to handle the intialization of the data: enum ArrayMemoryInit { InitializeMemory, NoInitializeMemory } auto x = new(NoInitializeMemory) char[512]; -SteveThis surely should be available at a language level.It would be nice, but I'm not sure how it'd fit into the syntax. new(void) char[512] ? new void char[512] ?
Jun 27 2008
On Fri, 27 Jun 2008 21:22:50 +0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:"Koroskin Denis" <2korden gmail.com> wrote in message news:op.udevrrqpenyajd proton.creatstudio.intranet...Think different! Let's suppose we have the following: T[] t = new T[512]; // initialized to T.init Now how do I resize `t` so that it would now contain 1024 elements (and first 512 of them remain the same)? t.length = 1024; // nope. initializes the rest of data There is a solution of my preference - introduce a resize method (yes, that's a proposal): import std.gc; T[] resize(T)(ref T[] array, uint newLength, bool doInit = true) { if (doInit) { array.length = newLength; return array; } return array.resizeUninited(newLength); } T[] resizeUninited(T)(ref T[] array, uint newLength) { return array = cast(T[])realloc(array.ptr, newLength * T.sizeof); } t.resize(1024, true); // initializes the data t.resize(1024); // a shortcut, same as above t.resize(1024, false); // here it is - that's what we need! t.resizeUninited(1024); // a shortcut So, back to the original question: "[how to] allocate unitialized data on the heap ?" My solution is to break it into two steps: T[] t; t.resize(1024, false); // or t.resizeUninited(1024, false); What do you think?This surely should be available at a language level.It would be nice, but I'm not sure how it'd fit into the syntax. new(void) char[512] ? new void char[512] ?
Jun 27 2008