Manual Memory Management

Allocate and manage memory manually.

Topics

First Steps

Calling Functions With Pointer Parameters

Use implicit pointer casting or bridging when calling functions that takes pointers as parameters.

Typed Pointers

Use typed pointers and buffers to access memory as instances of a specific type.

struct UnsafePointer

A pointer for accessing data of a specific type.

struct UnsafeMutablePointer

A pointer for accessing and manipulating data of a specific type.

struct UnsafeBufferPointer

A nonowning collection interface to a buffer of elements stored contiguously in memory.

struct UnsafeMutableBufferPointer

A nonowning collection interface to a buffer of mutable elements stored contiguously in memory.

Raw Pointers

Use raw pointers and buffers to access memory for loading and storing as raw bytes.

struct UnsafeRawPointer

A raw pointer for accessing untyped data.

struct UnsafeMutableRawPointer

A raw pointer for accessing and manipulating untyped data.

struct UnsafeRawBufferPointer

A nonowning collection interface to the bytes in a region of memory.

struct UnsafeMutableRawBufferPointer

A mutable nonowning collection interface to the bytes in a region of memory.

Memory Access

func withUnsafePointer<T, Result>(to: T, (UnsafePointer<T>) -> Result) -> Result

Invokes the given closure with a pointer to the given argument.

func withUnsafePointer<T, Result>(to: inout T, (UnsafePointer<T>) -> Result) -> Result

Invokes the given closure with a pointer to the given argument.

func withUnsafeMutablePointer<T, Result>(to: inout T, (UnsafeMutablePointer<T>) -> Result) -> Result

Calls the given closure with a mutable pointer to the given argument.

func withUnsafeBytes<T, Result>(of: T, (UnsafeRawBufferPointer) -> Result) -> Result

Invokes the given closure with a buffer pointer covering the raw bytes of the given argument.

func withUnsafeBytes<T, Result>(of: inout T, (UnsafeRawBufferPointer) -> Result) -> Result

Invokes the given closure with a buffer pointer covering the raw bytes of the given argument.

func withUnsafeMutableBytes<T, Result>(of: inout T, (UnsafeMutableRawBufferPointer) -> Result) -> Result

Invokes the given closure with a mutable buffer pointer covering the raw bytes of the given argument.

func swap<T>(inout T, inout T)

Exchanges the values of the two arguments.

Memory Layout

enum MemoryLayout

The memory layout of a type, describing its size, stride, and alignment.

Reference Counting

struct Unmanaged

A type for propagating an unmanaged object reference.

func withExtendedLifetime<T, Result>(T, (T) -> Result) -> Result

Evaluates a closure while ensuring that the given instance is not destroyed before the closure returns.

func withExtendedLifetime<T, Result>(T, () -> Result) -> Result

Evaluates a closure while ensuring that the given instance is not destroyed before the closure returns.

See Also

Programming Tasks

Input and Output

Print values to the console, read from and write to text streams, and use command line arguments.

Debugging and Reflection

Fortify your code with runtime checks, and examine your values' runtime representation.

Key-Path Expressions

Use key-path expressions to access properties dynamically.

Type Casting and Existential Types

Perform casts between types or represent values of any type.

C Interoperability

Use imported C types or call C variadic functions.

Operator Declarations

Work with prefix, postfix, and infix operators.