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

# allocate(capacity:)

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

```
static func allocate(capacity count: Int) -> UnsafeMutablePointer<Pointee>
```

## Parameters

`count`

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

## Discussion

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

The following example allocates enough new memory to store four `Int`
instances and then initializes that memory with the elements of a range.

```
let intPointer = UnsafeMutablePointer<Int>.allocate(capacity: 4)
for i in 0..<4 {
    (intPointer + i).initialize(to: i)
}
print(intPointer.pointee)
// Prints "0"
```

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

```
intPointer.deallocate()
```

You must only use `deallocate()` to end the lifetime of memory
created with `allocate()`; it is a programming error to use `free` or
another deallocation API, and may result in undefined behavior.

---

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)