Thread overview
Unitialized allocation
Jun 27, 2008
Mael
Jun 27, 2008
Koroskin Denis
Jun 27, 2008
Koroskin Denis
June 27, 2008
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 ?

June 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.


June 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...
>> 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.
June 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] ?


June 27, 2008
"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


June 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...
>>
>> 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?