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).

unsafe_pointer

Implements unsafe pointer types for manual memory management.

This module provides UnsafePointer and related type aliases for direct memory manipulation with explicit control over mutability, origins, and address spaces. It includes the alloc() function for heap allocation and comprehensive methods for loading, storing, and managing pointer lifetimes. These types enable low-level memory operations, interfacing with C code, and building custom data structures.

comptime values

ImmOpaquePointer

comptime ImmOpaquePointer[origin: ImmOrigin, *, address_space: AddressSpace = AddressSpace.GENERIC] = Pointer[NoneType, origin, address_space=address_space]

An immutable opaque pointer, equivalent to the C const void* type.

Parameters

  • origin (ImmOrigin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

ImmUnsafePointer

comptime ImmUnsafePointer[T: AnyType, origin: ImmOrigin, *, address_space: AddressSpace = AddressSpace.GENERIC] = Pointer[T, origin, address_space=address_space]

An immutable unsafe pointer.

Parameters

  • T (AnyType): The pointee type.
  • origin (ImmOrigin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

MutOpaquePointer

comptime MutOpaquePointer[origin: MutOrigin, *, address_space: AddressSpace = AddressSpace.GENERIC] = Pointer[NoneType, origin, address_space=address_space]

A mutable opaque pointer, equivalent to the C void* type.

Parameters

  • origin (MutOrigin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

MutUnsafePointer

comptime MutUnsafePointer[T: AnyType, origin: MutOrigin, *, address_space: AddressSpace = AddressSpace.GENERIC] = Pointer[T, origin, address_space=address_space]

A mutable unsafe pointer.

Parameters

  • T (AnyType): The pointee type.
  • origin (MutOrigin): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

OpaquePointer

comptime OpaquePointer[mut: Bool, //, origin: Origin[mut=mut], *, address_space: AddressSpace = AddressSpace.GENERIC] = Pointer[NoneType, origin, address_space=address_space]

An opaque pointer, equivalent to the C (const) void* type.

Parameters

  • mut (Bool): Whether the pointer is mutable.
  • origin (Origin[mut=mut]): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

OptionalPointer

comptime OptionalPointer[mut: Bool, //, T: AnyType, origin: Origin[mut=mut], *, address_space: AddressSpace = AddressSpace.GENERIC] = Optional[Pointer[T, origin, address_space=address_space]]

An optional (nullable) safe Pointer.

Parameters

  • mut (Bool): The mutability of the pointer.
  • T (AnyType): The type of the pointee.
  • origin (Origin[mut=mut]): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

OptionalUnsafePointer

comptime OptionalUnsafePointer[mut: Bool, //, T: AnyType, origin: Origin[mut=mut], *, address_space: AddressSpace = AddressSpace.GENERIC] = Optional[Pointer[T, origin, address_space=address_space]]

An optional (nullable) UnsafePointer.

Parameters

  • mut (Bool): The mutability of the pointer.
  • T (AnyType): The type of the pointee.
  • origin (Origin[mut=mut]): The origin of the pointer.
  • address_space (AddressSpace): The address space of the pointer.

UnsafePointer

comptime UnsafePointer[mut: Bool, //, T: AnyType, origin: Origin[mut=mut], *, address_space: AddressSpace = AddressSpace.GENERIC] = Pointer[T, origin, address_space=address_space]

An indirect reference to one or more values of T consecutively in memory, and can refer to uninitialized memory.

Parameters

  • mut (Bool): Whether the pointer is mutable.
  • T (AnyType): The type the pointer points to.
  • origin (Origin[mut=mut]): The origin of the memory being addressed.
  • address_space (AddressSpace): The address space of the pointer.

Structs

  • Pointer: Pointer represents an indirect reference to one or more values of type T consecutively in memory, and can refer to uninitialized memory.