<!--
{
  "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/UnsafeMutableBufferPointer/allocate(capacity:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SrsRi_zrlE8allocate8capacitySryxGSi_tFZ"
  },
  "title" : "allocate(capacity:)"
}
-->

# allocate(capacity:)

Allocates uninitialized memory for the specified number of instances of
type `Element`.

```
static func allocate(capacity count: Int) -> UnsafeMutableBufferPointer<Element>
```

## Parameters

`count`

The amount of memory to allocate, counted in instances
of `Element`.

## Discussion

The resulting buffer references a region of memory that is bound to
`Element` and is `count * MemoryLayout<Element>.stride` bytes in size.

The following example allocates a buffer that can store four `Int`
instances and then initializes that memory with the elements of a range:

```
let buffer = UnsafeMutableBufferPointer<Int>.allocate(capacity: 4)
_ = buffer.initialize(from: 1...4)
print(buffer[2])
// Prints "3"
```

When you allocate memory, always remember to deallocate once you’re
finished.

```
buffer.deallocate()
```

---

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)