struct
UnsafeMutablePointer
A pointer for accessing and manipulating data of a specific type.
Associated Types |
|
---|
Initializers
Creates an immutable typed pointer referencing the same memory as the given mutable pointer.
- Parameter other: The pointer to convert.
Declaration
public
init
(
_
other
:
UnsafeMutablePointer
<
Pointee
>
)
Creates a mutable typed pointer referencing the same memory as the given immutable pointer.
- Parameter other: The immutable pointer to convert.
Declaration
public
init
(
mutating
other
:
UnsafePointer
<
Pointee
>
)
Creates an immutable typed pointer referencing the same memory as the given mutable pointer.
- Parameter other: The pointer to convert. If
other
isnil
, the result isnil
.
Declaration
public
init
?(
_
other
:
UnsafeMutablePointer
<
Pointee
>
?)
Creates a mutable typed pointer referencing the same memory as the given immutable pointer.
- Parameter other: The immutable pointer to convert. If
other
isnil
, the result isnil
.
Declaration
public
init
?(
mutating
other
:
UnsafePointer
<
Pointee
>
?)
Instance Variables
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
Instance Methods
Replaces this pointer's initialized memory with the specified number of instances from the given pointer's memory.
The region of memory starting at this pointer and covering count
instances of the pointer's Pointee
type must be initialized or
Pointee
must be a trivial type. After calling
assign(from:count:)
, the region is initialized.
Note: Returns without performing work if
self
andsource
are equal.
Declaration
@
inlinable
public
func
assign
(
from
source
:
UnsafePointer
<
Pointee
>
,
count
:
Int
)
Replaces this pointer's memory with the specified number of consecutive copies of the given value.
The region of memory starting at this pointer and covering count
instances of the pointer's Pointee
type must be initialized or
Pointee
must be a trivial type. After calling
assign(repeating:count:)
, the region is initialized.
Declaration
@
inlinable
public
func
assign
(
repeating
repeatedValue
:
Pointee
,
count
:
Int
)
Deallocates the memory block previously allocated at this pointer.
This pointer must be a pointer to the start of a previously allocated memory
block. The memory must not be initialized or Pointee
must be a trivial type.
Declaration
@
inlinable
public
func
deallocate
()
Deinitializes the specified number of values starting at this pointer.
The region of memory starting at this pointer and covering count
instances of the pointer's Pointee
type must be initialized. After
calling deinitialize(count:)
, the memory is uninitialized, but still
bound to the Pointee
type.
- Parameter count: The number of instances to deinitialize.
count
must not be negative.
Declaration
@
inlinable
public
func
deinitialize
(
count
:
Int
) -
>
UnsafeMutableRawPointer
Initializes the memory referenced by this pointer with the values starting at the given pointer.
The region of memory starting at this pointer and covering count
instances of the pointer's Pointee
type must be uninitialized or
Pointee
must be a trivial type. After calling
initialize(from:count:)
, the region is initialized.
Declaration
@
inlinable
public
func
initialize
(
from
source
:
UnsafePointer
<
Pointee
>
,
count
:
Int
)
Initializes this pointer's memory with the specified number of consecutive copies of the given value.
The destination memory must be uninitialized or the pointer's Pointee
must be a trivial type. After a call to initialize(repeating:count:)
, the
memory referenced by this pointer is initialized.
Declaration
@
inlinable
public
func
initialize
(
repeating
repeatedValue
:
Pointee
,
count
:
Int
)
Initializes this pointer's memory with a single instance of the given value.
The destination memory must be uninitialized or the pointer's Pointee
must be a trivial type. After a call to initialize(to:)
, the
memory referenced by this pointer is initialized. Calling this method is
roughly equivalent to calling initialize(repeating:count:)
with a
count
of 1.
Declaration
@
inlinable
public
func
initialize
(
to
value
:
Pointee
)
Retrieves and returns the referenced instance, returning the pointer's memory to an uninitialized state.
Calling the move()
method on a pointer p
that references memory of
type T
is equivalent to the following code, aside from any cost and
incidental side effects of copying and destroying the value:
let
value
:
T
= {
defer
{
p
.
deinitialize
(
count
:
1
) }
return
p
.
pointee
}()
The memory referenced by this pointer must be initialized. After calling
move()
, the memory is uninitialized.
Declaration
@
inlinable
public
func
move
() -
>
Pointee
Replaces the memory referenced by this pointer with the values starting at the given pointer, leaving the source memory uninitialized.
The region of memory starting at this pointer and covering count
instances of the pointer's Pointee
type must be initialized or
Pointee
must be a trivial type. After calling
moveAssign(from:count:)
, the region is initialized and the memory
region source..<(source + count)
is uninitialized.
Declaration
@
inlinable
public
func
moveAssign
(
from
source
:
UnsafeMutablePointer
<
Pointee
>
,
count
:
Int
)
Moves instances from initialized source memory into the uninitialized memory referenced by this pointer, leaving the source memory uninitialized and the memory referenced by this pointer initialized.
The region of memory starting at this pointer and covering count
instances of the pointer's Pointee
type must be uninitialized or
Pointee
must be a trivial type. After calling
moveInitialize(from:count:)
, the region is initialized and the memory
region source..<(source + count)
is uninitialized.
Declaration
@
inlinable
public
func
moveInitialize
(
from
source
:
UnsafeMutablePointer
<
Pointee
>
,
count
:
Int
)
Executes the given closure while temporarily binding the specified number of instances to the given type.
Use this method when you have a pointer to memory bound to one type and
you need to access that memory as instances of another type. Accessing
memory as a type T
requires that the memory be bound to that type. 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.
The region of memory starting at this pointer and covering count
instances of the pointer's Pointee
type must be initialized.
The following example temporarily rebinds the memory of a UInt64
pointer to Int64
, then accesses a property on the signed integer.
let
uint64Pointer
:
UnsafeMutablePointer
<
UInt64
>
=
fetchValue
()
let
isNegative
=
uint64Pointer
.
withMemoryRebound
(
to
:
Int64
.
self
) {
ptr
in
return
ptr
.
pointee
<
0
}
Because this pointer's memory is no longer bound to its Pointee
type
while the body
closure executes, do not access memory using the
original pointer from within body
. Instead, use the body
closure's
pointer argument to access the values in memory as instances of type
T
.
After executing body
, this method rebinds memory back to the original
Pointee
type.
Note: Only use this method to rebind the pointer's memory to a type with the same size and stride as the currently bound
Pointee
type. To bind a region of memory to a type that is a different size, convert the pointer to a raw pointer and use thebindMemory(to:capacity:)
method.
Declaration
@
inlinable
public
func
withMemoryRebound
<
T
,
Result
>
(
to
type
:
T
.
Type
,
capacity
count
:
Int
,
_
body
: (
UnsafeMutablePointer
<
T
>
)
throws
-
>
Result
)
rethrows
-
>
Result
Type Methods
Allocates uninitialized memory for the specified number of instances of
type Pointee
.
The resulting pointer references a region of memory that is bound to
Pointee
and is count * MemoryLayout<Pointee>.stride
bytes in size.
The following example allocates enough new memory to store four Int
instances and then initializes that memory with the elements of a range.
let
intPointer
=
UnsafeMutablePointer
<
Int
>
.
allocate
(
capacity
:
4
)
for
i
in
0
..
<
4
{
(
intPointer
+
i
).
initialize
(
to
:
i
)
}
(
intPointer
.
pointee
)
// Prints "0"
When you allocate memory, always remember to deallocate once you're finished.
intPointer
.
deallocate
()
- Parameter count: The amount of memory to allocate, counted in instances
of
Pointee
.
Declaration
@
inlinable
public
static
func
allocate
(
capacity
count
:
Int
) -
>
UnsafeMutablePointer
<
Pointee
>
You use instances of the
UnsafeMutablePointer
type to access data of a specific type in memory. The type of data that a pointer can access is the pointer'sPointee
type.UnsafeMutablePointer
provides no automated memory management or 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
UnsafeMutablePointer
type to access and manage memory that has been bound to a specific type.Understanding a Pointer's Memory State
The memory referenced by an
UnsafeMutablePointer
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.Uninitialized Memory
Memory that has just been allocated through a typed pointer or has been deinitialized is in an uninitialized state. Uninitialized memory must be initialized before it can be accessed for reading.
You can use methods like
initialize(to:count:)
,initialize(from:count:)
, andmoveInitialize(from:count:)
to initialize the memory referenced by a pointer with a value or series of values.Initialized Memory
Initialized memory has a value that can be read using a pointer's
pointee
property or through subscript notation. In the following example,ptr
is a pointer to memory initialized with a value of23
:Accessing a Pointer's Memory as a Different Type
When you access memory through an
UnsafeMutablePointer
instance, thePointee
type must be consistent with the bound type of the memory. If you do need to access memory that is bound to one type as a different type, Swift's pointer types provide type-safe ways to temporarily or permanently change the bound type of the memory, or to load typed instances directly from raw memory.An
UnsafeMutablePointer<UInt8>
instance allocated with eight bytes of memory,uint8Pointer
, will be used for the examples below.When you only need to temporarily access a pointer's memory as a different type, use the
withMemoryRebound(to:capacity:)
method. For example, you can use this method to call an API that expects a pointer to a different type that is layout compatible with your pointer'sPointee
. The following code temporarily rebinds the memory thatuint8Pointer
references fromUInt8
toInt8
to call the imported Cstrlen
function.When you need to permanently rebind memory to a different type, first obtain a raw pointer to the memory and then call the
bindMemory(to:capacity:)
method on the raw pointer. The following example binds the memory referenced byuint8Pointer
to one instance of theUInt64
type:After rebinding the memory referenced by
uint8Pointer
toUInt64
, accessing that pointer's referenced memory as aUInt8
instance is undefined.Alternatively, you can access the same memory as a different type without rebinding through untyped memory access, so long as the bound type and the destination type are trivial types. Convert your pointer to an
UnsafeMutableRawPointer
instance and then use the raw pointer'sload(fromByteOffset:as:)
andstoreBytes(of:toByteOffset:as:)
methods to read and write values.Performing Typed Pointer Arithmetic
Pointer arithmetic with a typed pointer is counted in strides of the pointer's
Pointee
type. When you add to or subtract from anUnsafeMutablePointer
instance, the result is a new pointer of the same type, offset by that number of instances of thePointee
type.You can also use subscript notation to access the value in memory at a specific offset.
Implicit Casting and Bridging
When calling a function or method with an
UnsafeMutablePointer
parameter, you can pass an instance of that specific pointer type or use Swift's implicit bridging to pass a compatible pointer.For example, the
printInt(atAddress:)
function in the following code sample expects anUnsafeMutablePointer<Int>
instance as its first parameter:As is typical in Swift, you can call the
printInt(atAddress:)
function with anUnsafeMutablePointer
instance. This example passesintPointer
, a mutable pointer to anInt
value, toprint(address:)
.Alternatively, you can use Swift's implicit bridging to pass a pointer to an instance or to the elements of an array. The following example passes a pointer to the
value
variable by using inout syntax:A mutable pointer to the elements of an array is implicitly created when you pass the array using inout syntax. This example uses implicit bridging to pass a pointer to the elements of
numbers
when callingprintInt(atAddress:)
.No matter which way you call
printInt(atAddress:)
, Swift's type safety guarantees that you can only pass a pointer to the type required by the function---in this case, a pointer to anInt
.