UnsafeMutablePointer

struct UnsafeMutablePointer<Pointee>

A raw pointer for accessing data of type Pointee. This type provides no automated memory management, and therefore must be handled with great care to ensure safety.

Instances must be aligned to MemoryLayout<Pointee>.alignment, i.e. (UnsafePointer<Int8>(self) - nil) % MemoryLayout<Pointee>.alignment == 0

The memory referenced by an instance can be in one of the following states:

  • Memory is not allocated (for example, pointer is null, or memory has been deallocated previously).

  • Memory is allocated, but value has not been initialized.

  • Memory is allocated and value is initialized.

Inheritance CVarArg, Comparable, CustomDebugStringConvertible, CustomPlaygroundQuickLookable, CustomReflectable, Equatable, Hashable, Strideable View Protocol Hierarchy →
Associated Types
Distance = Int
Import import Swift

Initializers

init(_: OpaquePointer)

Converts from an opaque pointer to a typed pointer.

Declaration

init(_ from: OpaquePointer)
init(_: UnsafeMutablePointer<Pointee>)

Creates an UnsafeMutablePointer from another UnsafeMutablePointer.

Declaration

init(_ other: UnsafeMutablePointer<Pointee>)
init(mutating:)

Converts from UnsafePointer to UnsafeMutablePointer of the same Pointee.

Declaration

init(mutating other: UnsafePointer<Pointee>)
init?(_: OpaquePointer?)

Converts from an opaque pointer to a typed pointer.

Returns nil if from is nil.

Declaration

init?(_ from: OpaquePointer?)
init?(_: UnsafeMutablePointer<Pointee>?)

Creates an UnsafeMutablePointer from another UnsafeMutablePointer.

Returns nil if other is nil.

Declaration

init?(_ other: UnsafeMutablePointer<Pointee>?)
init?(bitPattern: Int)

Creates an UnsafeMutablePointer with a given pattern of bits.

Returns nil if bitPattern is zero.

Declaration

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

Creates an UnsafeMutablePointer with a given pattern of bits.

Returns nil if bitPattern is zero.

Declaration

init?(bitPattern: UInt)
init?(mutating:)

Converts from UnsafePointer to UnsafeMutablePointer of the same Pointee.

Returns nil if bitPattern is zero.

Declaration

init?(mutating other: UnsafePointer<Pointee>?)

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 }
var pointee: Pointee

Accesses the Pointee instance referenced by self.

Precondition: the pointee has been initialized with an instance of type Pointee.

Declaration

var pointee: Pointee { get set }

Subscripts

subscript(_: Int)

Accesses the pointee at self + i.

Precondition: the pointee at self + i is initialized.

Declaration

subscript(i: Int) -> Pointee { get nonmutating set }

Static Methods

static func allocate(capacity:)

Allocates and points at uninitialized aligned memory for count instances of Pointee.

Postcondition: The pointee is allocated, but not initialized.

Declaration

static func allocate(capacity count: Int) -> UnsafeMutablePointer<Pointee>

Instance Methods

func advanced(by:)

Returns self + n.

Declaration

func advanced(by n: Int) -> UnsafeMutablePointer<Pointee>
func assign(from:count:)

Replaces count initialized Pointees starting at self with the count Pointees at source.

Precondition: count >= 0

Precondition: The Pointees at self..<self + count and source..<source + count are initialized.

Declaration

func assign(from source: UnsafePointer<Pointee>, count: Int)
func deallocate(_:)

Deallocates uninitialized memory allocated for count instances of Pointee.

Precondition: The memory is not initialized.

Postcondition: The memory has been deallocated.

Declaration

func deallocate(capacity: Int)
func deinitialize(_:)

De-initializes the count Pointees starting at self, returning their memory to an uninitialized state.

Returns an UnsafeMutableRawPointer to this memory.

Precondition: The Pointees at self..<self + count are initialized.

Postcondition: The memory is uninitialized.

Declaration

func deinitialize(count: Int = default) -> UnsafeMutableRawPointer
func distance(to:)

Returns end - self.

Declaration

func distance(to x: UnsafeMutablePointer<Pointee>) -> Int
func initialize(from:)

Initializes memory starting at self with the elements of source.

Precondition: The memory at self..<self + count is uninitialized.

Postcondition: The Pointees at self..<self + count are initialized.

Declaration

func initialize<C : Collection where C.Iterator.Element == Pointee>(from source: C)
func initialize(from:count:)

Initializes memory starting at self with count Pointees beginning at source.

Precondition: count >= 0

Precondition: The memory regions source..<source + count and self..<self + count do not overlap.

Precondition: The memory at self..<self + count is uninitialized and the Pointees at source..<source + count are initialized.

Postcondition: The Pointees at self..<self + count and source..<source + count are initialized.

Declaration

func initialize(from source: UnsafePointer<Pointee>, count: Int)
func initialize(to:count:)

Initializes self.pointee with count consecutive copies of newValue

Precondition: The pointee is not initialized.

Precondition: count is non-negative.

Postcondition: The pointee is initialized; the value should eventually be destroyed or moved from to avoid leaks.

Declaration

func initialize(to newValue: Pointee, count: Int = default)
func move()

Retrieves the pointee, returning the referenced memory to an uninitialized state.

Equivalent to { defer { deinitialize() }; return pointee }(), but more efficient.

Precondition: The pointee is initialized.

Postcondition: The memory is uninitialized.

Declaration

func move() -> Pointee
func moveAssign(from:count:)

Replaces count initialized Pointees starting at self with the count Pointees starting at source, returning the source memory to an uninitialized state.

Precondition: count >= 0

Precondition: The memory regions source..<source + count and self..<self + count do not overlap.

Precondition: The Pointees at self..<self + count and source..<source + count are initialized.

Postcondition: The Pointees at self..<self + count are initialized and the Pointees at source..<source + count are uninitialized.

Declaration

func moveAssign(from source: UnsafeMutablePointer<Pointee>, count: Int)
func moveInitialize(from:count:)

Initializes memory starting at self with count Pointees beginning at source, and returning the source memory to an uninitialized state.

Precondition: count >= 0

Precondition: The memory at self..<self + count is uninitialized and the Pointees at source..<source + count are initialized.

Postcondition: The Pointees at self..<self + count are initialized and the memory at source..<source + count is uninitialized.

Declaration

func moveInitialize(from source: UnsafeMutablePointer<Pointee>, count: Int)
func predecessor()

Returns the previous consecutive position.

Declaration

func predecessor() -> UnsafeMutablePointer<Pointee>
func successor()

Returns the next consecutive position.

Declaration

func successor() -> UnsafeMutablePointer<Pointee>
func withMemoryRebound(_:capacity:_:)

Rebinds memory at self to type T with capacity to hold count adjacent T values while executing the body closure.

After executing the closure, rebinds memory back to Pointee.

Precondition: Type 'T' is layout compatible with type 'Pointee'. Precondition: The memory self..<self + count * MemoryLayout<T>.stride is bound to Pointee.

Accessing UnsafeMutablePointer<T>.pointee requires that the memory be "bound" to type T. A memory location may only be bound to one type at a time, so accessing the same memory as an unrelated type without first rebinding the memory is undefined. self may not be accessed within the body closure because memory is no longer bound to Pointee while it executes. The closure's UnsafeMutablePointer<T> argument must not escape the closure because memory is only temporarily bound to T.

To persistently bind this memory to a different type, first obtain a raw pointer to the memory, then invoke the bindMemory API: UnsafeRawPointer(typedPointer).bindMemory(to:capacity:).

Declaration

func withMemoryRebound<T, Result>(to: T.Type, capacity count: Int, _ body: (UnsafeMutablePointer<T>) throws -> Result) rethrows -> Result