Thread overview
2D arrays, slices and manual memory allocation
Feb 25, 2022
Nonobvious
Feb 25, 2022
9il
Feb 25, 2022
Nonobvious
February 25, 2022

From Go Your Own Way (Part Two: The Heap):

import core.stdc.stdlib;

`// Allocate a block of untyped bytes that can be managed`
`// as a slice.`
`void[] allocate(size_t size)`
`{`
    `// malloc(0) is implementation defined (might return null `
    `// or an address), but is almost certainly not what we want.`
    `assert(size != 0);`

    `void* ptr = malloc(size);`
    `if(!ptr) assert(0, "Out of memory!");`

    `// Return a slice of the pointer so that the address is coupled`
    `// with the size of the memory block.`
    `return ptr[0 .. size];`
}

T[] allocArray(T)(size_t count)
{
// Make sure to account for the size of the
// array element type!
return cast(T[])allocate(T.sizeof * count);
}

What is the equivalent for higher dimensional arrays?

February 25, 2022

On Friday, 25 February 2022 at 06:03:34 UTC, Nonobvious wrote:

>

From Go Your Own Way (Part Two: The Heap):

[...]

http://mir-algorithm.libmir.org/mir_ndslice_allocation.html#.stdcUninitSlice

February 25, 2022

On Friday, 25 February 2022 at 06:13:35 UTC, 9il wrote:

>

On Friday, 25 February 2022 at 06:03:34 UTC, Nonobvious wrote:

>

From Go Your Own Way (Part Two: The Heap):

[...]

http://mir-algorithm.libmir.org/mir_ndslice_allocation.html#.stdcUninitSlice

Thank you.