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

# initializeMemory(as:repeating:)

Initializes the memory referenced by this buffer slice with the given
value, binds the memory to the value’s type, and returns a typed
buffer of the initialized memory.

```
@discardableResult func initializeMemory<T>(as type: T.Type, repeating repeatedValue: T) -> UnsafeMutableBufferPointer<T>
```

## Parameters

`type`

The type to bind this buffer’s memory to.

`repeatedValue`

The instance to copy into memory.

## Return Value

A typed buffer of the memory referenced by this raw buffer.
The typed buffer contains `self.count / MemoryLayout<T>.stride`
instances of `T`.

## Discussion

The memory referenced by this buffer slice must be uninitialized or
initialized to a trivial type, and must be properly aligned for
accessing `T`.

After calling this method on a raw buffer slice referencing memory
starting at `b = base.baseAddress + startIndex`,
the region starting at `b` and continuing up to
`b + self.count - self.count % MemoryLayout<T>.stride` is bound
to type `T` and is initialized. If `T` is a nontrivial type, you must
eventually deinitialize or move the values in this region to avoid leaks.
If `base.baseAddress` is `nil`, this function does nothing
and returns an empty buffer pointer.

---

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)