For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
alloc
alloc[T: AnyType, //](layout: Layout[T]) -> UnsafePointer[T, MutExternalOrigin]
Allocates contiguous storage for layout.count() elements of T.
The allocation uses the alignment specified by layout. Use free with the
same Layout to release the memory.
Example:
from std.memory.alloc import *
var layout = Layout[Int32](count=4, alignment=64)
var ptr = alloc(layout)
for i in range(layout.count()):
(ptr + i).init_pointee_move(i)
free(ptr, layout)
Constraints:
size_of[T]() must be greater than zero. layout.count() must be
greater than zero.
Parameters:
- T (
AnyType): The type of the elements to allocate storage for.
Args:
- layout (
Layout[T]): Describes the number of elements and alignment of the allocation.
Returns:
UnsafePointer[T, MutExternalOrigin]: A pointer to the newly allocated uninitialized storage.