<!--
{
  "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/ManagedBufferPointer",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s20ManagedBufferPointerV"
  },
  "title" : "ManagedBufferPointer"
}
-->

# ManagedBufferPointer

Contains a buffer object, and provides access to an instance of
`Header` and contiguous storage for an arbitrary number of
`Element` instances stored in that buffer.

```
@frozen struct ManagedBufferPointer<Header, Element> where Element : ~Copyable
```

## Overview

For most purposes, the `ManagedBuffer` class can be used on its own.
However, in cases
where objects of various different classes must serve as storage,
you need to also use `ManagedBufferPointer`.

A valid buffer class is non-`@objc`, with no declared stored
properties.  Its `deinit` must destroy its
stored `Header` and any constructed `Element`s.

## Example Buffer Class

```
 class MyBuffer<Element> { // non-@objc
   typealias Manager = ManagedBufferPointer<(Int, String), Element>
   deinit {
     Manager(unsafeBufferObject: self).withUnsafeMutablePointers {
       (pointerToHeader, pointerToElements) -> Void in
       pointerToElements.deinitialize(count: self.count)
       pointerToHeader.deinitialize(count: 1)
     }
   }

   // All properties are *computed* based on members of the Header
   var count: Int {
     return Manager(unsafeBufferObject: self).header.0
   }
   var name: String {
     return Manager(unsafeBufferObject: self).header.1
   }
 }
```

## Topics

### Creating a Buffer

[`init(bufferClass:minimumCapacity:makingHeaderWith:)`](/documentation/Swift/ManagedBufferPointer/init(bufferClass:minimumCapacity:makingHeaderWith:))

Create with new storage containing an initial `Header` and space
for at least `minimumCapacity` `element`s.

[`init(unsafeBufferObject:)`](/documentation/Swift/ManagedBufferPointer/init(unsafeBufferObject:))

Manage the given `buffer`.

### Inspecting a Buffer

[`capacity`](/documentation/Swift/ManagedBufferPointer/capacity)

The actual number of elements that can be stored in this object.

[`header`](/documentation/Swift/ManagedBufferPointer/header)

The stored `Header` instance.

[`buffer`](/documentation/Swift/ManagedBufferPointer/buffer)

Returns the object instance being used for storage.

[`isUniqueReference()`](/documentation/Swift/ManagedBufferPointer/isUniqueReference())

Returns `true` if `self` holds the only strong reference to its
buffer; otherwise, returns `false`.

### Accessing Buffer Contents

[`withUnsafeMutablePointerToElements(_:)`](/documentation/Swift/ManagedBufferPointer/withUnsafeMutablePointerToElements(_:))

Call `body` with an `UnsafeMutablePointer` to the `Element`
storage.

[`withUnsafeMutablePointerToHeader(_:)`](/documentation/Swift/ManagedBufferPointer/withUnsafeMutablePointerToHeader(_:))

Call `body` with an `UnsafeMutablePointer` to the stored
`Header`.

[`withUnsafeMutablePointers(_:)`](/documentation/Swift/ManagedBufferPointer/withUnsafeMutablePointers(_:))

Call `body` with `UnsafeMutablePointer`s to the stored `Header`
and raw `Element` storage.

### Comparing Buffers

[`!=(_:_:)`](/documentation/Swift/ManagedBufferPointer/!=(_:_:))

Returns a Boolean value indicating whether two values are not equal.

## Relationships

### Conforms To

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

---

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)