Generic Instance Method

bindMemory(to:capacity:)

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

Declaration

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

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.

Return Value

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.

Discussion

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.