digitalmars.D.learn - 2D arrays, slices and manual memory allocation
- Nonobvious (26/26) Feb 24 2022 From [Go Your Own Way (Part Two: The
- 9il (2/5) Feb 24 2022 http://mir-algorithm.libmir.org/mir_ndslice_allocation.html#.stdcUninitS...
- Nonobvious (2/8) Feb 24 2022 Thank you.
From [Go Your Own Way (Part Two: The Heap)](https://dlang.org/blog/2017/09/25/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?
Feb 24 2022
On Friday, 25 February 2022 at 06:03:34 UTC, Nonobvious wrote:From [Go Your Own Way (Part Two: The Heap)](https://dlang.org/blog/2017/09/25/go-your-own-way-part-two-the-heap/): [...]
Feb 24 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:Thank you.From [Go Your Own Way (Part Two: The Heap)](https://dlang.org/blog/2017/09/25/go-your-own-way-part-two-the-heap/): [...]
Feb 24 2022