UnsafeMutablePointer

struct UnsafeMutablePointer<T>

A pointer to an object of type T. This type provides no automated memory management, and therefore the user must take care to allocate and free memory appropriately.

The pointer 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 BidirectionalIndexType, CVarArgType, Comparable, DebugPrintable, Equatable, ForwardIndexType, Hashable, NilLiteralConvertible, RandomAccessIndexType, Reflectable, SinkType, Strideable, _BidirectionalIndexType, _Comparable, _ForwardIndexType, _Incrementable, _PointerType, _RandomAccessIndexType, _Strideable View Protocol Hierarchy →
Import import Swift

Initializers

init()

Construct a null pointer.

Declaration

init()
init(_: COpaquePointer)

Convert from an opaque C pointer to a typed pointer

This is a fundamentally unsafe conversion.

Declaration

init(_ other: COpaquePointer)
init<U>(_: UnsafeMutablePointer<U>)

Convert from an UnsafeMutablePointer of a different type.

This is a fundamentally unsafe conversion.

Declaration

init<U>(_ from: UnsafeMutablePointer<U>)
init<U>(_: UnsafePointer<U>)

Convert from a UnsafePointer of a different type.

This is a fundamentally unsafe conversion.

Declaration

init<U>(_ from: UnsafePointer<U>)
init(bitPattern: UWord)

Construct an UnsafeMutablePointer from a given address in memory.

This is a fundamentally unsafe conversion.

Declaration

init(bitPattern: UWord)
init(bitPattern: Word)

Construct an UnsafeMutablePointer from a given address in memory.

This is a fundamentally unsafe conversion.

Declaration

init(bitPattern: Word)
init(nilLiteral:)

Create an instance initialized with nil.

Declaration

init(nilLiteral: ())

Instance Variables

var debugDescription: String

A textual representation of self, suitable for debugging.

Declaration

var debugDescription: String { get }
var hashValue: Int

The hash value.

Axiom: x == y implies x.hashValue == y.hashValue

Note: 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 memory: T

Access the underlying raw memory, getting and setting values.

Declaration

var memory: T { get set }

Subscripts

subscript(_: Int)

Declaration

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

Static Methods

static func alloc(_:)

Allocate memory for num objects of type T.

Postcondition: the memory is allocated, but not initialized.

Declaration

static func alloc(num: Int) -> UnsafeMutablePointer<T>

Instance Methods

func advancedBy(_:)

Return self offset by n steps.

Returns: If n > 0, the result of applying successor to self n times. If n < 0, the result of applying predecessor to self -n times. Otherwise, self.

Complexity: O(1)

Declaration

func advancedBy(n: Int) -> UnsafeMutablePointer<T>
func assignBackwardFrom(_:count:)

Assign from count values beginning at source into initialized memory, proceeding from the last value to the first. Use this for assigning ranges into later memory that may overlap with the source range.

Requires: either source precedes self or follows self + count.

Declaration

func assignBackwardFrom(source: UnsafeMutablePointer<T>, count: Int)
func assignFrom(_:count:)

Assign from count values beginning at source into initialized memory, proceeding from the first element to the last.

Declaration

func assignFrom(source: UnsafeMutablePointer<T>, count: Int)
func dealloc(_:)

Deallocate num objects.

num number of objects to deallocate. Should match exactly the value that was passed to alloc() (partial deallocations are not possible).

Precondition: the memory is not initialized.

Postcondition: the memory has been deallocated.

Declaration

func dealloc(num: Int)
func destroy()

Destroy the object the pointer points to.

Precondition: the memory is initialized.

Postcondition: the value has been destroyed and the memory must be initialized before being used again.

Declaration

func destroy()
func destroy(_:)

Destroy the count objects the pointer points to. Precondition: the memory is initialized.

Postcondition: the value has been destroyed and the memory must be initialized before being used again.

Declaration

func destroy(count: Int)
func distanceTo(_:)

Return the minimum number of applications of successor or predecessor required to reach other from self.

Complexity: O(1).

Declaration

func distanceTo(x: UnsafeMutablePointer<T>) -> Int
func encode()

Transform self into a series of machine words that can be appropriately interpreted by C varargs

Declaration

func encode() -> [Word]
func getMirror()

Returns a mirror that reflects self.

Declaration

func getMirror() -> MirrorType
func initialize(_:)

Initialize the value the pointer points to, to construct an object where there was no object previously stored.

Precondition: the memory is not initialized.

Postcondition: the memory is initalized; the value should eventually be destroyed or moved from to avoid leaks.

Declaration

func initialize(newvalue: T)
func initializeFrom(_:)

Copy the elements of C into raw memory.

Precondition: the memory is not initialized.

Declaration

func initializeFrom<C : CollectionType where T == T>(source: C)
func initializeFrom(_:count:)

Copy count values beginning at source into raw memory.

Precondition: the memory is not initialized.

Requires: self and source may not overlap.

Declaration

func initializeFrom(source: UnsafeMutablePointer<T>, count: Int)
func move()

Retrieve the value the pointer points to, moving it away from the location referenced in memory.

Equivalent to reading memory property and calling destroy(), but more efficient.

Precondition: the memory is initialized.

Postcondition: the value has been destroyed and the memory must be initialized before being used again.

Declaration

func move() -> T
func moveAssignFrom(_:count:)

Assign from count values beginning at source into initialized memory, transforming the source values into raw memory.

Requires: the self and source ranges may not overlap.

Declaration

func moveAssignFrom(source: UnsafeMutablePointer<T>, count: Int)
func moveInitializeBackwardFrom(_:count:)

Move count values beginning at source into uninitialized memory, transforming the source values into raw memory, proceeding from the last value to the first. Use this for copying ranges into later memory that may overlap with the source range.

Requires: either source precedes self or follows self + count.

Declaration

func moveInitializeBackwardFrom(source: UnsafeMutablePointer<T>, count: Int)
func moveInitializeFrom(_:count:)

Move count values beginning at source into raw memory, transforming the source values into raw memory.

Declaration

func moveInitializeFrom(source: UnsafeMutablePointer<T>, count: Int)
func predecessor()

Returns the previous consecutive value before self.

Requires: the previous value is representable.

Declaration

func predecessor() -> UnsafeMutablePointer<T>
mutating func put(_:)

Declaration

mutating func put(x: T)
func successor()

Returns the next consecutive value after self.

Requires: the next value is representable.

Declaration

func successor() -> UnsafeMutablePointer<T>