---
title: "withTemporaryAllocation(byteCount:alignment:_:)"
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(bytecount:alignment:_:)"
---

# withTemporaryAllocation(byteCount:alignment:_:)

Provides scoped access to an output raw span with the specified byte count and alignment.

## Declaration

```swift
func withTemporaryAllocation<R, E>(byteCount: Int, alignment: Int, _ body: @_lifetime(0: copy 0) (inout OutputRawSpan) throws(E) -> R) throws(E) -> R where E : Error, R : ~Copyable
```

## Parameters

- `byteCount`: The number of bytes to temporarily allocate. byteCount must not be negative.
- `alignment`: The alignment of the new, temporary region of allocated memory, in bytes. alignment must be a whole power of 2.
- `body`: A closure to invoke and to which the allocated output raw span should be passed.

## Return Value

Return Value Whatever is returned by body.

## Discussion

Discussion This function is useful for cheaply allocating raw storage 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 OutputRawSpan. body may append bytes to the output raw span. After body returns, deallocation is 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(of:capacity:_:)](swift/withtemporaryallocation(of:capacity:_:).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)
