<!--
{
  "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/UnsafeMutableRawBufferPointer/initializeMemory(as:from:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sw16initializeMemory2as4from8IteratorQz9unwritten_Sry7ElementQzG11initializedtAHm_xtSTRzlF"
  },
  "title" : "initializeMemory(as:from:)"
}
-->

# initializeMemory(as:from:)

Initializes the buffer’s memory with the given elements, binding the
initialized memory to the elements’ type.

```
func initializeMemory<S>(as type: S.Element.Type, from source: S) -> (unwritten: S.Iterator, initialized: UnsafeMutableBufferPointer<S.Element>) where S : Sequence
```

## Parameters

`type`

The type of element to which this buffer’s memory will be bound.

`source`

A sequence of elements with which to initialize the buffer.

## Return Value

An iterator to any elements of `source` that didn’t fit in the
buffer, and a typed buffer of the written elements. The returned
buffer references memory starting at the same base address as this
buffer.

## Discussion

When calling the `initializeMemory(as:from:)` method on a buffer `b`,
the memory referenced by `b` must be uninitialized or initialized to a
trivial type, and must be properly aligned for accessing `S.Element`.
The buffer must contain sufficient memory to accommodate
`source.underestimatedCount`.

This method initializes the buffer with elements from `source` until
`source` is exhausted or, if `source` is a sequence but not a collection,
the buffer has no more room for source’s elements. After calling
`initializeMemory(as:from:)`, the memory referenced by the returned
`UnsafeMutableBufferPointer` instance is bound and initialized to type
`S.Element`. This method does not change
the binding state of the unused portion of `b`, if any.

---

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)