Skip to main content

Mojo function

alloc

alloc[type: AnyType, /](count: Int, *, alignment: Int = align_of[type]()) -> UnsafePointerV2[type, origin_of()]

Allocates contiguous storage for count elements of type with alignment alignment.

Safety:

  • The returned memory is uninitialized; reading before writing is undefined.
  • The returned pointer has an empty mutable origin; you must call free() to release it.

Example:

var p = alloc[Int32](4)
p.store(0, Int32(42))
p.store(1, Int32(7))
p.store(2, Int32(9))
var a = p.load(0)
print(a[0], p.load(1)[0], p.load(2)[0])
p.free()

Constraints:

count must be positive and size_of[type]() must be > 0.

Parameters:

  • โ€‹type (AnyType): The type of the elements to allocate storage for.

Args:

  • โ€‹count (Int): Number of elements to allocate.
  • โ€‹alignment (Int): The alignment of the allocation.

Returns:

UnsafePointerV2: Pointer to the newly allocated uninitialized array.

Was this page helpful?