UnsafeRawPointer

struct UnsafeRawPointer

A raw pointer for accessing untyped data.

The UnsafeRawPointer type provides no automated memory management, no type safety, and no alignment guarantees. You are responsible for handling the life cycle of any memory you work with through unsafe pointers, to avoid leaks or undefined behavior.

Memory that you manually manage can be either untyped or bound to a specific type. You use the UnsafeRawPointer type to access and manage raw bytes in memory, whether or not that memory has been bound to a specific type.

Understanding a Pointer's Memory State

The memory referenced by an UnsafeRawPointer instance can be in one of several states. Many pointer operations must only be applied to pointers with memory in a specific state---you must keep track of the state of the memory you are working with and understand the changes to that state that different operations perform. Memory can be untyped and uninitialized, bound to a type and uninitialized, or bound to a type and initialized to a value. Finally, memory that was allocated previously may have been deallocated, leaving existing pointers referencing unallocated memory.

Raw, Uninitialized Memory

Raw memory that has just been allocated is in an uninitialized, untyped state. Uninitialized memory must be initialized with values of a type before it can be used with any typed operations.

To bind uninitialized memory to a type without initializing it, use the bindMemory(to:count:) method. This method returns a typed pointer for further typed access to the memory.

Typed Memory

Memory that has been bound to a type, whether it is initialized or uninitialized, is typically accessed using typed pointers---instances of UnsafePointer and UnsafeMutablePointer. Initialization, assignment, and deinitialization can be performed using UnsafeMutablePointer methods.

Memory that has been bound to a type can be rebound to a different type only after it has been deinitialized or if the bound type is a trivial type. Deinitializing typed memory does not unbind that memory's type. The deinitialized memory can be reinitialized with values of the same type, bound to a new type, or deallocated.

Note: A trivial type can be copied bit for bit with no indirection or reference-counting operations. Generally, native Swift types that do not contain strong or weak references or other forms of indirection are trivial, as are imported C structs and enumerations.

When reading from memory as raw bytes when that memory is bound to a type, you must ensure that you satisfy any alignment requirements.

Raw Pointer Arithmetic

Pointer arithmetic with raw pointers is performed at the byte level. When you add to or subtract from a raw pointer, the result is a new raw pointer offset by that number of bytes. The following example allocates four bytes of memory and stores 0xFF in all four bytes:

let bytesPointer = UnsafeMutableRawPointer.allocate(byteCount: 4, alignment: 1)
bytesPointer.storeBytes(of: 0xFFFF_FFFF, as: UInt32.self)

// Load a value from the memory referenced by 'bytesPointer'
let x = bytesPointer.load(as: UInt8.self)       // 255

// Load a value from the last two allocated bytes
let offsetPointer = bytesPointer + 2
let y = offsetPointer.load(as: UInt16.self)     // 65535

The code above stores the value 0xFFFF_FFFF into the four newly allocated bytes, and then loads the first byte as a UInt8 instance and the third and fourth bytes as a UInt16 instance.

Always remember to deallocate any memory that you allocate yourself.

bytesPointer.deallocate()

Implicit Casting and Bridging

When calling a function or method with an UnsafeRawPointer parameter, you can pass an instance of that specific pointer type, pass an instance of a compatible pointer type, or use Swift's implicit bridging to pass a compatible pointer.

For example, the print(address:as:) function in the following code sample takes an UnsafeRawPointer instance as its first parameter:

func print<T>(address p: UnsafeRawPointer, as type: T.Type) {
    let value = p.load(as: type)
    print(value)
}

As is typical in Swift, you can call the print(address:as:) function with an UnsafeRawPointer instance. This example passes rawPointer as the initial parameter.

// 'rawPointer' points to memory initialized with `Int` values.
let rawPointer: UnsafeRawPointer = ...
print(address: rawPointer, as: Int.self)
// Prints "42"

Because typed pointers can be implicitly cast to raw pointers when passed as a parameter, you can also call print(address:as:) with any mutable or immutable typed pointer instance.

let intPointer: UnsafePointer<Int> = ...
print(address: intPointer, as: Int.self)
// Prints "42"

let mutableIntPointer = UnsafeMutablePointer(mutating: intPointer)
print(address: mutableIntPointer, as: Int.self)
// Prints "42"

Alternatively, you can use Swift's implicit bridging to pass a pointer to an instance or to the elements of an array. Use inout syntax to implicitly create a pointer to an instance of any type. The following example uses implicit bridging to pass a pointer to value when calling print(address:as:):

var value: Int = 23
print(address: &value, as: Int.self)
// Prints "23"

An immutable pointer to the elements of an array is implicitly created when you pass the array as an argument. This example uses implicit bridging to pass a pointer to the elements of numbers when calling print(address:as:).

let numbers = [5, 10, 15, 20]
print(address: numbers, as: Int.self)
// Prints "5"

You can also use inout syntax to pass a mutable pointer to the elements of an array. Because print(address:as:) requires an immutable pointer, although this is syntactically valid, it isn't necessary.

var mutableNumbers = numbers
print(address: &mutableNumbers, as: Int.self)

Important: The pointer created through implicit bridging of an instance or of an array's elements is only valid during the execution of the called function. Escaping the pointer to use after the execution of the function is undefined behavior. In particular, do not use implicit bridging when calling an UnsafeRawPointer initializer.

  var number = 5
  let numberPointer = UnsafeRawPointer(&number)
  // Accessing 'numberPointer' is undefined behavior.
Inheritance Comparable, CustomDebugStringConvertible, CustomPlaygroundQuickLookable, CustomReflectable, Equatable, Hashable, Strideable View Protocol Hierarchy →
Import import Swift

Initializers

init(_: OpaquePointer)

Creates a new raw pointer from the given opaque pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The opaque pointer to convert.

Declaration

init(_ other: OpaquePointer)
init(_: UnsafeMutableRawPointer)

Creates a new raw pointer from the given mutable raw pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The mutable raw pointer to convert.

Declaration

init(_ other: UnsafeMutableRawPointer)
init(_: UnsafeRawPointer)

Creates a new raw pointer from the given raw or typed pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The pointer to convert.

Declaration

init(_ other: UnsafeRawPointer)
init<T>(_: AutoreleasingUnsafeMutablePointer<T>)

Creates a new raw pointer from an AutoreleasingUnsafeMutablePointer instance.

other: The pointer to convert.

Declaration

init<T>(_ other: AutoreleasingUnsafeMutablePointer<T>)
init<T>(_: UnsafeMutablePointer<T>)

Creates a new raw pointer from the given typed pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The typed pointer to convert.

Declaration

init<T>(_ other: UnsafeMutablePointer<T>)
init<T>(_: UnsafePointer<T>)

Creates a new raw pointer from the given typed pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The typed pointer to convert.

Declaration

init<T>(_ other: UnsafePointer<T>)
init?(_: OpaquePointer?)

Creates a new raw pointer from the given opaque pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The opaque pointer to convert. If other is nil, the result is nil.

Declaration

init?(_ other: OpaquePointer?)
init?(_: UnsafeMutableRawPointer?)

Creates a new raw pointer from the given mutable raw pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The mutable raw pointer to convert. If other is nil, the result is nil.

Declaration

init?(_ other: UnsafeMutableRawPointer?)
init?(_: UnsafeRawPointer?)

Creates a new raw pointer from the given raw or typed pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The pointer to convert. If other is nil, the result is nil.

Declaration

init?(_ other: UnsafeRawPointer?)
init?<T>(_: AutoreleasingUnsafeMutablePointer<T>?)

Creates a new raw pointer from an AutoreleasingUnsafeMutablePointer instance.

other: The pointer to convert. If other is nil, the result is nil.

Declaration

init?<T>(_ other: AutoreleasingUnsafeMutablePointer<T>?)
init?<T>(_: UnsafeMutablePointer<T>?)

Creates a new raw pointer from the given typed pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The typed pointer to convert. If other is nil, the result is nil.

Declaration

init?<T>(_ other: UnsafeMutablePointer<T>?)
init?<T>(_: UnsafePointer<T>?)

Creates a new raw pointer from the given typed pointer.

Use this initializer to explicitly convert other to an UnsafeRawPointer instance. This initializer creates a new pointer to the same address as other and performs no allocation or copying.

other: The typed pointer to convert. If other is nil, the result is nil.

Declaration

init?<T>(_ other: UnsafePointer<T>?)
init?(bitPattern: Int)

Creates a new raw pointer from the given address, specified as a bit pattern.

bitPattern: A bit pattern to use for the address of the new raw pointer. If bitPattern is zero, the result is nil.

Declaration

init?(bitPattern: Int)
init?(bitPattern: UInt)

Creates a new raw pointer from the given address, specified as a bit pattern.

bitPattern: A bit pattern to use for the address of the new raw pointer. If bitPattern is zero, the result is nil.

Declaration

init?(bitPattern: UInt)

Instance Variables

var customMirror: Mirror

The custom mirror for this instance.

If this type has value semantics, the mirror should be unaffected by subsequent mutations of the instance.

Declaration

var customMirror: Mirror { get }
var customPlaygroundQuickLook: PlaygroundQuickLook

A custom playground Quick Look for this instance.

If this type has value semantics, the PlaygroundQuickLook instance should be unaffected by subsequent mutations.

Declaration

var customPlaygroundQuickLook: PlaygroundQuickLook { get }
var debugDescription: String

A textual representation of the pointer, suitable for debugging.

Declaration

var debugDescription: String { get }
var hashValue: Int

The pointer's hash value.

The hash value is not guaranteed to be stable across different invocations of the same program. Do not persist the hash value across program runs.

Declaration

var hashValue: Int { get }

Instance Methods

func advanced(by:)

Returns a pointer offset from this pointer by the specified number of bytes.

With pointer p and distance n, the result of p.advanced(by: n) is equivalent to p + n.

The resulting pointer must be within the bounds of the same allocation as this pointer.

n: The number of bytes to offset this pointer. n may be positive, negative, or zero. Returns: A pointer offset from this pointer by n bytes.

Declaration

func advanced(by n: Int) -> UnsafeRawPointer
func assumingMemoryBound(_:)

Returns a typed pointer to the memory referenced by this pointer, assuming that the memory is already bound to the specified type.

Use this method when you have a raw pointer to memory that has already been bound to the specified type. The memory starting at this pointer must be bound to the type T. Accessing memory through the returned pointer is undefined if the memory has not been bound to T. To bind memory to T, use bindMemory(to:capacity:) instead of this method.

to: The type T that the memory has already been bound to. Returns: A typed pointer to the same memory as this raw pointer.

Declaration

func assumingMemoryBound<T>(to: T.Type) -> UnsafePointer<T>
func bindMemory(to:capacity:)

Binds the memory to the specified type and returns a typed pointer to the bound memory.

Use the bindMemory(to:capacity:) method to bind the memory referenced by this pointer to the type T. The memory must be uninitialized or initialized to a type that is layout compatible with T. If the memory is uninitialized, it is still uninitialized after being bound to T.

In this example, 100 bytes of raw memory are allocated for the pointer bytesPointer, and then the first four bytes are bound to the Int8 type.

let count = 4
let bytesPointer = UnsafeMutableRawPointer.allocate(
        bytes: 100,
        alignedTo: MemoryLayout<Int8>.alignment)
let int8Pointer = bytesPointer.bindMemory(to: Int8.self, capacity: count)

After calling bindMemory(to:capacity:), the first four bytes of the memory referenced by bytesPointer are bound to the Int8 type, though they remain uninitialized. The remainder of the allocated region is unbound raw memory. All 100 bytes of memory must eventually be deallocated.

Warning: A memory location may only be bound to one type at a time. The behavior of accessing memory as a type unrelated to its bound type is undefined.

Parameters: type: The type T to bind the memory to. count: The amount of memory to bind to type T, counted as instances of T. Returns: A typed pointer to the newly bound memory. The memory in this region is bound to T, but has not been modified in any other way. The number of bytes in this region is count * MemoryLayout<T>.stride.

Declaration

func bindMemory<T>(to type: T.Type, capacity count: Int) -> UnsafePointer<T>
func deallocate()

Deallocates the previously allocated memory block referenced by this pointer.

The memory to be deallocated must be uninitialized or initialized to a trivial type.

Declaration

func deallocate()
func deallocate(bytes:alignedTo:)

Declaration

func deallocate(bytes _: Int, alignedTo _: Int)
func distance(to:)

Returns the distance from this pointer to the given pointer.

With pointers p and q, the result of p.distance(to: q) is equivalent to q - p.

x: The pointer to calculate the distance to. Returns: The distance from this pointer to x, in bytes.

Declaration

func distance(to x: UnsafeRawPointer) -> Int
func load(fromByteOffset:as:)

Returns a new instance of the given type, constructed from the raw memory at the specified offset.

The memory at this pointer plus offset must be properly aligned for accessing T and initialized to T or another type that is layout compatible with T.

Parameters: offset: The offset from this pointer, in bytes. offset must be nonnegative. The default is zero. type: The type of the instance to create. Returns: A new instance of type T, read from the raw bytes at offset. The returned instance is memory-managed and unassociated with the value in the memory referenced by this pointer.

Declaration

func load<T>(fromByteOffset offset: Int = default, as type: T.Type) -> T

Conditionally Inherited Items

The initializers, methods, and properties listed below may be available on this type under certain conditions (such as methods that are available on Array when its elements are Equatable) or may not ever be available if that determination is beyond SwiftDoc.org's capabilities. Please open an issue on GitHub if you see something out of place!

Where Stride : SignedInteger

func ...(_:maximum:)

Returns a countable closed range that contains both of its bounds.

Use the closed range operator (...) to create a closed range of any type that conforms to the Strideable protocol with an associated signed integer Stride type, such as any of the standard library's integer types. This example creates a ClosedRange<Int> from zero up to, and including, nine.

let singleDigits = 0...9
print(singleDigits.contains(9))
// Prints "true"

You can use sequence or collection methods on the singleDigits range.

print(singleDigits.count)
// Prints "10"
print(singleDigits.last)
// Prints "9"

Parameters:)`. minimum: The lower bound for the range. maximum: The upper bound for the range.

Declaration

func ...(minimum: UnsafeRawPointer, maximum: UnsafeRawPointer) -> ClosedRange<UnsafeRawPointer>

Declared In

Strideable