---
title: "withTemporaryAllocation(of:capacity:_:)"
framework: Swift
role: symbol
role_heading: Function
platforms: [iOS 12.2+, iPadOS 12.2+, Mac Catalyst 12.2+, macOS 10.14.4+, tvOS 12.2+, visionOS 1.0+, watchOS 5.2+]
path: "swift/withtemporaryallocation(of:capacity:_:)"
---

# withTemporaryAllocation(of:capacity:_:)

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

## Declaration

```swift
func withTemporaryAllocation<T, R, E>(of type: T.Type, capacity: Int, _ body: @_lifetime(0: copy 0) (inout OutputSpan<T>) throws(E) -> R) throws(E) -> R where E : Error, T : ~Copyable, R : ~Copyable
```

## Parameters

- `type`: The type of the elements in the buffer being temporarily allocated.
- `capacity`: The capacity of the output span being temporarily allocated.
- `body`: A closure to invoke and to which the allocated output span should be passed.

## Return Value

Return Value Whatever is returned by body.

## Discussion

Discussion This function is useful for cheaply allocating storage for a sequence of values for a brief duration. Storage may be allocated on the heap or on the stack, depending on the required size and alignment. When body is called, it is passed an empty OutputSpan. body may append or initialize elements in the output span. Any elements that have been initialized when body returns are deinitialized automatically, and deallocation is also automatic. note: Whatever is thrown by body.

## See Also

### Memory Access

- [withUnsafePointer(to:_:)](swift/withunsafepointer(to:_:)-9fjn6.md)
- [withUnsafePointer(to:_:)](swift/withunsafepointer(to:_:)-35wrn.md)
- [withUnsafeMutablePointer(to:_:)](swift/withunsafemutablepointer(to:_:).md)
- [withUnsafeBytes(of:_:)](swift/withunsafebytes(of:_:)-3ywhh.md)
- [withUnsafeMutableBytes(of:_:)](swift/withunsafemutablebytes(of:_:).md)
- [withTemporaryAllocation(byteCount:alignment:_:)](swift/withtemporaryallocation(bytecount:alignment:_:).md)
- [withUnsafeTemporaryAllocation(of:capacity:_:)](swift/withunsafetemporaryallocation(of:capacity:_:).md)
- [withUnsafeTemporaryAllocation(byteCount:alignment:_:)](swift/withunsafetemporaryallocation(bytecount:alignment:_:).md)
- [swap(_:_:)](swift/swap(_:_:).md)
- [exchange(_:with:)](swift/exchange(_:with:).md)
