<!--
{
  "documentType" : "article",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/manual-memory-management",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "Manual Memory Management"
}
-->

# Manual Memory Management

Allocate and manage memory manually.

## Topics

### First Steps

  <doc://com.apple.Swift/documentation/Swift/calling-functions-with-pointer-parameters>

### Safe Memory Access

[`Span`](/documentation/Swift/Span)

`Span<Element>` represents a contiguous region of memory
which contains initialized instances of `Element`.

[`RawSpan`](/documentation/Swift/RawSpan)

`RawSpan` represents a contiguous region of memory
which contains initialized bytes.

[`OutputSpan`](/documentation/Swift/OutputSpan)

`OutputSpan` is a reference to a contiguous region of memory that starts
with some number of initialized `Element` instances followed by
uninitialized memory. It provides operations to access the items it stores,
as well as to add new elements and to remove existing ones.

[`OutputRawSpan`](/documentation/Swift/OutputRawSpan)

`OutputRawSpan` is a reference to a contiguous region of memory which starts
with some number of initialized bytes, followed by uninitialized memory.
It provides operations to access the bytes it stores,
as well as to append and to remove bytes.

[`UTF8Span`](/documentation/Swift/UTF8Span)

A borrowed view into contiguous memory that contains validly-encoded UTF-8 code units.

[`MutableSpan`](/documentation/Swift/MutableSpan)

`MutableSpan<Element>` represents a contiguous region of memory which
contains initialized instances of `Element`.

[`MutableRawSpan`](/documentation/Swift/MutableRawSpan)

`MutableRawSpan` represents a contiguous region of memory
which contains initialized bytes.

### Safe Access to Raw Bytes

[`FullyInhabited`](/documentation/Swift/FullyInhabited)

A protocol for types whose memory can safely be written as or read from
raw bytes.

[`ConvertibleFromBytes`](/documentation/Swift/ConvertibleFromBytes)

A protocol for types whose memory can safely be populated from raw bytes,
resulting in a valid instance.

[`ConvertibleToBytes`](/documentation/Swift/ConvertibleToBytes)

A protocol for types whose memory can safely be read as individual raw bytes.

[`ByteOrder`](/documentation/Swift/ByteOrder)

A byte ordering in memory.

[`bitCast(_:to:)`](/documentation/Swift/bitCast(_:to:))

Returns the bits of the given instance, interpreted as having the specified
type.

### Typed Pointers

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

[`UnsafePointer`](/documentation/Swift/UnsafePointer)

A pointer for accessing data of a specific type.

[`UnsafeMutablePointer`](/documentation/Swift/UnsafeMutablePointer)

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

[`UnsafeBufferPointer`](/documentation/Swift/UnsafeBufferPointer)

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

[`UnsafeMutableBufferPointer`](/documentation/Swift/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.

[`UnsafeRawPointer`](/documentation/Swift/UnsafeRawPointer)

A raw pointer for accessing untyped data.

[`UnsafeMutableRawPointer`](/documentation/Swift/UnsafeMutableRawPointer)

A raw pointer for accessing and manipulating untyped data.

[`UnsafeRawBufferPointer`](/documentation/Swift/UnsafeRawBufferPointer)

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

[`UnsafeMutableRawBufferPointer`](/documentation/Swift/UnsafeMutableRawBufferPointer)

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

### Memory Access

[`withUnsafePointer(to:_:)`](/documentation/Swift/withUnsafePointer(to:_:)-9fjn6)

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

[`withUnsafePointer(to:_:)`](/documentation/Swift/withUnsafePointer(to:_:)-35wrn)

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

[`withUnsafeMutablePointer(to:_:)`](/documentation/Swift/withUnsafeMutablePointer(to:_:))

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

[`withUnsafeBytes(of:_:)`](/documentation/Swift/withUnsafeBytes(of:_:)-3ywhh)

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

[`withUnsafeMutableBytes(of:_:)`](/documentation/Swift/withUnsafeMutableBytes(of:_:))

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

[`withTemporaryAllocation(byteCount:alignment:_:)`](/documentation/Swift/withTemporaryAllocation(byteCount:alignment:_:))

Provides scoped access to an output raw span with the specified byte count
and alignment.

[`withTemporaryAllocation(of:capacity:_:)`](/documentation/Swift/withTemporaryAllocation(of:capacity:_:))

Provides scoped access to an output span of the specified type and capacity.

[`withUnsafeTemporaryAllocation(of:capacity:_:)`](/documentation/Swift/withUnsafeTemporaryAllocation(of:capacity:_:))

Provides scoped access to a buffer pointer to memory of the specified type
and with the specified capacity.

[`withUnsafeTemporaryAllocation(byteCount:alignment:_:)`](/documentation/Swift/withUnsafeTemporaryAllocation(byteCount:alignment:_:))

Provides scoped access to a raw buffer pointer with the specified byte count
and alignment.

[`swap(_:_:)`](/documentation/Swift/swap(_:_:))

Exchanges the values of the two arguments.

[`exchange(_:with:)`](/documentation/Swift/exchange(_:with:))

Replaces the value of a mutable value with the supplied new value,
returning the original.

### Memory Layout

[`MemoryLayout`](/documentation/Swift/MemoryLayout)

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

### Heap Storage

[`UniqueArray`](/documentation/Swift/UniqueArray)

A dynamically self-resizing, heap allocated, noncopyable array of
potentially noncopyable elements.

[`UniqueBox`](/documentation/Swift/UniqueBox)

A smart pointer type that uniquely owns an instance of `Value` on the heap.

### Reference Counting

[`Unmanaged`](/documentation/Swift/Unmanaged)

A type for propagating an unmanaged object reference.

[`withExtendedLifetime(_:_:)`](/documentation/Swift/withExtendedLifetime(_:_:)-4mmpv)

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

[`withExtendedLifetime(_:_:)`](/documentation/Swift/withExtendedLifetime(_:_:)-59dz3)

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

[`extendLifetime(_:)`](/documentation/Swift/extendLifetime(_:))

Extends the lifetime of the given instance.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)