<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UnsafeBufferPointer",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SR"
  },
  "title" : "UnsafeBufferPointer"
}
-->

# UnsafeBufferPointer

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

```
@frozen struct UnsafeBufferPointer<Element> where Element : ~Copyable
```

## Overview

You can use an `UnsafeBufferPointer` instance in low level operations to eliminate
uniqueness checks and, in release mode, bounds checks. Bounds checks are
always performed in debug mode.

# UnsafeBufferPointer Semantics

An `UnsafeBufferPointer` instance is a view into memory and does not own the memory
that it references. Copying a value of type `UnsafeBufferPointer` does not copy the
instances stored in the underlying memory. However, initializing another
collection with an `UnsafeBufferPointer` instance copies the instances out of the
referenced memory and into the new collection.

## Topics

### Initializers

[`init(MLMultiArray) throws`](/documentation/Swift/UnsafeBufferPointer/init(_:)-2fjdq)

[`init(UnsafeMutableBufferPointer<Element>)`](/documentation/Swift/UnsafeBufferPointer/init(_:)-36fvm)

Creates an immutable typed buffer pointer referencing the same memory as the
given mutable buffer pointer.

[`init(AudioBuffer)`](/documentation/Swift/UnsafeBufferPointer/init(_:)-5ei4s)

Initialize an `UnsafeBufferPointer<Element>` from an `AudioBuffer`.
Binds the buffer’s memory type to `Element`.

[`init(rebasing: Slice<UnsafeMutableBufferPointer<Element>>)`](/documentation/Swift/UnsafeBufferPointer/init(rebasing:)-53eec)

Creates a buffer over the same memory as the given buffer slice.

[`init(rebasing: Slice<UnsafeBufferPointer<Element>>)`](/documentation/Swift/UnsafeBufferPointer/init(rebasing:)-56rdb)

Creates a buffer over the same memory as the given buffer slice.

[`init(start: UnsafePointer<Element>?, count: Int)`](/documentation/Swift/UnsafeBufferPointer/init(start:count:))

Creates a new buffer pointer over the specified number of contiguous
instances beginning at the given pointer.

### Instance Properties

[`var baseAddress: UnsafePointer<Element>?`](/documentation/Swift/UnsafeBufferPointer/baseAddress)

A pointer to the first element of the buffer.

[`let count: Int`](/documentation/Swift/UnsafeBufferPointer/count)

The number of elements in the buffer.

[`var span: Span<Element>`](/documentation/Swift/UnsafeBufferPointer/span)

A span over the elements of this buffer.

### Instance Methods

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

Deallocates the memory block previously allocated at this buffer pointer’s
base address.

[`func extracting(some RangeExpression<Int>) -> UnsafeBufferPointer<Element>`](/documentation/Swift/UnsafeBufferPointer/extracting(_:)-47z4z)

Constructs a standalone buffer pointer over the items within the supplied
range of positions in the memory region addressed by this buffer pointer.

[`func extracting(UnboundedRange) -> UnsafeBufferPointer<Element>`](/documentation/Swift/UnsafeBufferPointer/extracting(_:)-4bn8q)

Extracts and returns a copy of the entire buffer.

[`func extracting(Range<Int>) -> UnsafeBufferPointer<Element>`](/documentation/Swift/UnsafeBufferPointer/extracting(_:)-nivx)

Constructs a standalone buffer pointer over the items within the supplied
range of positions in the memory region addressed by this buffer pointer.

[`func isTriviallyIdentical(to: UnsafeBufferPointer<Element>) -> Bool`](/documentation/Swift/UnsafeBufferPointer/isTriviallyIdentical(to:))

Returns a Boolean value indicating whether two instances refer to the same
memory region.

[`func withMemoryRebound<T, E, Result>(to: T.Type, (UnsafeBufferPointer<T>) throws(E) -> Result) throws(E) -> Result`](/documentation/Swift/UnsafeBufferPointer/withMemoryRebound(to:_:))

Executes the given closure while temporarily binding the memory referenced
by this buffer to the given type.

### Subscripts

[`subscript(Int) -> Element`](/documentation/Swift/UnsafeBufferPointer/subscript(_:)-3sy16)

Accesses the element at the specified position.

### Default Implementations

[AtomicRepresentable Implementations](/documentation/Swift/UnsafeBufferPointer/AtomicRepresentable-Implementations)

[BidirectionalCollection Implementations](/documentation/Swift/UnsafeBufferPointer/BidirectionalCollection-Implementations)

[Collection Implementations](/documentation/Swift/UnsafeBufferPointer/Collection-Implementations)

[CustomDebugStringConvertible Implementations](/documentation/Swift/UnsafeBufferPointer/CustomDebugStringConvertible-Implementations)

[OperationParameter Implementations](/documentation/Swift/UnsafeBufferPointer/OperationParameter-Implementations)

[Sequence Implementations](/documentation/Swift/UnsafeBufferPointer/Sequence-Implementations)

## Relationships

### Conforms To

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

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

[`OperationParameter`](/documentation/Accelerate/BNNSGraph/Builder/OperationParameter)

[`DataProtocol`](/documentation/Foundation/DataProtocol)

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

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

[`ContiguousBytes`](/documentation/Foundation/ContiguousBytes)

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

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

[`AccelerateBuffer`](/documentation/Accelerate/AccelerateBuffer)

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

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

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

[`AtomicRepresentable`](/documentation/Synchronization/AtomicRepresentable)

---

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)