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 PointerA pointer for accessing data of a specific type.
struct Unsafe Mutable PointerA pointer for accessing and manipulating data of a specific type.
struct Unsafe Buffer PointerA nonowning collection interface to a buffer of elements stored contiguously in memory.
struct Unsafe Mutable Buffer PointerA 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 PointerA raw pointer for accessing untyped data.
struct Unsafe Mutable Raw PointerA raw pointer for accessing and manipulating untyped data.
struct Unsafe Raw Buffer PointerA nonowning collection interface to the bytes in a region of memory.
struct Unsafe Mutable Raw Buffer PointerA 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) -> ResultInvokes the given closure with a pointer to the given argument.
func with Unsafe Pointer<T, Result>(to: inout T, (Unsafe Pointer<T>) -> Result) -> ResultInvokes 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) -> ResultCalls the given closure with a mutable pointer to the given argument.
func with Unsafe Bytes<T, Result>(of: T, (Unsafe Raw Buffer Pointer) -> Result) -> ResultInvokes 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) -> ResultInvokes 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) -> ResultInvokes 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 LayoutThe memory layout of a type, describing its size, stride, and alignment.
Reference Counting
struct UnmanagedA type for propagating an unmanaged object reference.
func with Extended Lifetime<T, Result>(T, (T) -> Result) -> ResultEvaluates a closure while ensuring that the given instance is not destroyed before the closure returns.
func with Extended Lifetime<T, Result>(T, () -> Result) -> ResultEvaluates 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.