Allocate and manage memory manually.
Manual Memory Management
Topics
First Steps
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 Unsafe Pointer
A pointer for accessing data of a specific type.
struct Unsafe Mutable Pointer
A pointer for accessing and manipulating data of a specific type.
struct Unsafe Buffer Pointer
A nonowning collection interface to a buffer of elements stored contiguously in memory.
struct Unsafe Mutable Buffer Pointer
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 Unsafe Raw Pointer
A raw pointer for accessing untyped data.
struct Unsafe Mutable Raw Pointer
A raw pointer for accessing and manipulating untyped data.
struct Unsafe Raw Buffer Pointer
A nonowning collection interface to the bytes in a region of memory.
struct Unsafe Mutable Raw Buffer Pointer
A mutable nonowning collection interface to the bytes in a region of memory.
Memory Access
func with Unsafe Pointer<T, Result>(to: T, (Unsafe Pointer<T>) -> Result) -> Result
Invokes the given closure with a pointer to the given argument.
func with Unsafe Pointer<T, Result>(to: inout T, (Unsafe Pointer<T>) -> Result) -> Result
Invokes the given closure with a pointer to the given argument.
func with Unsafe Mutable Pointer<T, Result>(to: inout T, (Unsafe Mutable Pointer<T>) -> Result) -> Result
Calls the given closure with a mutable pointer to the given argument.
func with Unsafe Bytes<T, Result>(of: T, (Unsafe Raw Buffer Pointer) -> Result) -> Result
Invokes the given closure with a buffer pointer covering the raw bytes of the given argument.
func with Unsafe Bytes<T, Result>(of: inout T, (Unsafe Raw Buffer Pointer) -> Result) -> Result
Invokes the given closure with a buffer pointer covering the raw bytes of the given argument.
func with Unsafe Mutable Bytes<T, Result>(of: inout T, (Unsafe Mutable Raw Buffer Pointer) -> 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 Memory Layout
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 with Extended Lifetime<T, Result>(T, (T) -> Result) -> Result
Evaluates a closure while ensuring that the given instance is not destroyed before the closure returns.
func with Extended Lifetime<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
Print values to the console, read from and write to text streams, and use command line arguments.
Fortify your code with runtime checks, and examine your values' runtime representation.
Use key-path expressions to access properties dynamically.
Perform casts between types or represent values of any type.
Use imported C types or call C variadic functions.
Work with prefix, postfix, and infix operators.