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

pointer

Implements Pointer, Mojo's primary pointer type for indirect memory access.

Pointer is safe when constructed from and used to access an existing value. It also provides unsafe_-prefixed methods for working with dynamically-allocated or uninitialized memory; the caller is responsible for tracking initialization state and memory ownership.

You can import these APIs from the memory package. For example:

from std.memory import Pointer

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.

ImmPointer

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

An immutable 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.

MutPointer

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

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

Structs

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