IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
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).

free

free[T: AnyType, //](pointer: UnsafePointer[T, MutExternalOrigin], /, layout: Layout[T])

Frees memory previously allocated with alloc.

The layout argument must match the one passed to the corresponding alloc call. Passing a mismatched layout or a pointer not obtained from alloc is undefined behavior.

Safety:

  • pointer must have been returned by a matching call to alloc.
  • layout must equal the layout passed to that alloc call.

Example:

var layout = Layout[String].single()
var ptr = alloc(layout)

ptr.init_pointee_move("Dynamic allocation!")
ptr.destroy_pointee()

free(ptr, layout)

Parameters:

  • T (AnyType): The type of the elements in the allocation.

Args: