allocate(capacity:)
Allocates uninitialized memory for the specified number of instances of type Element.
Declaration
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()