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

# initializeMemory(as:fromContentsOf:)

Initializes the buffer slice’s memory with every element of the source,
binding the initialized memory to the elements’ type.

```
func initializeMemory<C>(as type: C.Element.Type, fromContentsOf source: C) -> UnsafeMutableBufferPointer<C.Element> where C : Collection
```

## Parameters

`type`

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

`source`

A collection of elements to be used to
initialize the buffer slice’s storage.

## Return Value

A typed buffer referencing the initialized elements.
The returned buffer references memory starting at the same
base address as this slice, and its count is equal to `source.count`

## Discussion

When calling the `initializeMemory(as:fromContentsOf:)` method,
the memory referenced by the buffer slice must be uninitialized,
or initialized to a trivial type. The buffer slice must reference
enough memory to store `source.count` elements, and it
must be properly aligned for accessing `C.Element`.

This method initializes the buffer with the contents of `source`
until `source` is exhausted.
After calling `initializeMemory(as:fromContentsOf:)`, the memory
referenced by the returned `UnsafeMutableBufferPointer` instance is bound
to the type of `C.Element` and is initialized. This method does not change
the binding state of the unused portion of the buffer slice, 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)