www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unitialized allocation

reply Mael <mael.primet gmail.com> writes:
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
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
parent reply "Koroskin Denis" <2korden gmail.com> writes:
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...
 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.
This surely should be available at a language level.
Jun 27 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Jarrett Billingsley" wrote
 "Koroskin Denis" wrote
 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] ?
New 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]; -Steve
Jun 27 2008
prev sibling parent "Koroskin Denis" <2korden gmail.com> writes:
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...
 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] ?
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?
Jun 27 2008