Login
📚 The Mojo Manual
Chapters ▾
⇩ Download ▾

6.1 Intro to pointers

A pointer is an indirect reference to one or more values stored in memory. The pointer is a value that holds an address to memory, and provides APIs to store and retrieve values to that memory. The value pointed to by a pointer is also known as a pointee.

The Mojo standard library includes several types of pointers, which provide different sets of features. All of these pointer types are generic—they can point to any type of value, and the value type is specified as a parameter. For example, the following code creates an OwnedPointer that points to an Int value:

from std.memory import OwnedPointer

var ptr: OwnedPointer[Int]
ptr = OwnedPointer(100)

The ptr variable has a value of type OwnedPointer[Int]. The pointer points to a value of type Int, as shown in Figure 1.

A local variable, ptr, points to an OwnedPointer[Int] which points to an Int pointee. The value of the OwnedPointer is the address of the Int pointee.

Accessing the memory—to retrieve or update a value—is called dereferencing the pointer. You can dereference a pointer by following the variable name with an empty pair of square brackets:

# Update an initialized value
ptr[] += 10
# Access an initialized value
print(ptr[])

Pointer terminology

Before we jump into the pointer types, here are a few terms you'll run across. Some of them may already be familiar to you.

Pointer types

The Mojo standard library includes several pointer types with different characteristics:

Table 1 summarizes the different types of pointers:

PointerOwnedPointerArcPointerUnsafePointer
SafeYesYesYesNo
Allocates memoryNoImplicitly 1Implicitly 1Explicitly
Owns pointee(s)NoYesYesNo 2
Implicitly copyableYesNoYesYes
NullableNoNoNoNo
Can point to uninitialized memoryNoNoNoYes
Can point to multiple values (array-like access)NoNoNoYes

1 OwnedPointer and ArcPointer implicitly allocate memory when you initialize the pointer with a value.

2 UnsafePointer provides unsafe methods for initializing and destroying instances of the stored type. The user is responsible for managing the lifecycle of stored values. <!-- rumdl-enable MD033 -->

The following sections provide more details on each pointer type.

Pointer

The Pointer type is a safe pointer that points to an initialized value that it doesn't own. Some example use cases for a Pointer include:

might hold a Pointer back to the original list.

external_call.

the iterators for standard library collection types like List return pointers.)

You can construct a Pointer to an existing value by calling the constructor with the to keyword argument:

from std.memory import Pointer

ptr = Pointer(to=some_value)

You can also create a Pointer by copying an existing Pointer.

A Pointer carries an origin for the stored value, so Mojo can track the lifetime of the referenced value.

OwnedPointer

The OwnedPointer type is a smart pointer designed for cases where there is single ownership of the underlying data. An OwnedPointer points to a single item, which is passed in when you initialize the OwnedPointer. The OwnedPointer allocates memory and moves or copies the value into the reserved memory.

from std.memory import OwnedPointer

o_ptr = OwnedPointer(some_big_struct^)

An owned pointer can hold almost any type of item, but when constructing an OwnedPointer, the stored item must be either Movable or Copyable.

Since an OwnedPointer is designed to enforce single ownership, the pointer itself can be moved, but not copied.

OwnedPointer does provide a constructor that creates a new OwnedPointer by copying the stored value from an existing OwnedPointer. This results in two owned pointers, each with its own separate allocation and its own copy of the stored value.

ArcPointer

An ArcPointer is a reference-counted smart pointer, ideal for shared resources where the last owner for a given value may not be clear. Like an OwnedPointer, it points to a single value, and it allocates memory when you initialize the ArcPointer with a value:

from std.memory import ArcPointer

var attributesDict: Dict[String, String] = {}
var attributes = ArcPointer(attributesDict^)

Unlike an OwnedPointer, an ArcPointer can be freely copied. All instances of a given ArcPointer share a reference count, which is incremented whenever the ArcPointer is copied and decremented whenever an instance is destroyed. When the reference count reaches zero, the stored value is destroyed and the allocated memory is freed.

You can use ArcPointer to implement safe reference-semantic types. For example, in the following code snippet SharedDict uses an ArcPointer to store a dictionary. Copying an instance of SharedDict only copies the ArcPointer, not the dictionary, which is shared between all of the copies.

from std.memory import ArcPointer

struct SharedDict(ImplicitlyCopyable):
    var attributes: ArcPointer[Dict[String, String]]

    def __init__(out self):
        var attributesDict: Dict[String, String] = {}
        self.attributes = ArcPointer(attributesDict^)

    def __init__(out self, *, copy: Self):
        self.attributes = copy.attributes

    def __setitem__(mut self, key: String, value: String):
        self.attributes[][key] = value

    def __getitem__(self, key: String) -> String:
        return self.attributes[].get(key, default="")

def main():
    thing1 = SharedDict()
    thing2 = thing1
    thing1["Flip"] = "Flop"
    print(thing2["Flip"])
Show output ✓ Verified · Mojo 1.0.0b2 (2cf4d08a)
Flop

UnsafePointer

UnsafePointer is a low-level pointer that can access a block of contiguous memory locations, which might be uninitialized. It's analogous to a raw pointer in the C and C++ programming languages. UnsafePointer provides unsafe methods for initializing and destroying stored values, as well as for accessing the values once they're initialized.

As the name suggests, UnsafePointer doesn't provide any memory safety guarantees, so you should reserve it for cases when none of the other pointer types will do the job. Here are some use cases where you might want to use an UnsafePointer:

For more information, see Unsafe pointers.