# (De)Initialization of Character inside of UnsafeMutableBufferPointer

**URL:** <https://forums.swift.org/t/de-initialization-of-character-inside-of-unsafemutablebufferpointer/81398>\
**Category:** Using Swift\
**Tags:** memory-safety, buffer, arc, unsafe\
**Created:** [July 30, 2025, 12:45pm UTC](https://forums.swift.org/t/de-initialization-of-character-inside-of-unsafemutablebufferpointer/81398 "2025-07-30T12:45:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex-ley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hex-ley/32/42336_2.png) [@hex-ley](https://forums.swift.org/u/hex-ley)\
**Post date:** [July 30, 2025, 12:45pm UTC](https://forums.swift.org/t/de-initialization-of-character-inside-of-unsafemutablebufferpointer/81398/1 "2025-07-30T12:45:48Z")

</div>

Hi everyone,

I'm currently implementing a custom data structure based on two buffers, where each holds the characters left and right to the cursor respectively. Sure I could just use `[Character]` arrays instead. But since I also know C I wanted to learn how to manually manage memory in Swift, and hopefully squeeze out some extra performance.

My current approach is to store two `UnsafeMutableBufferPointer<Character>` inside a non copyable struct. Memory allocation would happen in the initializer, and deallocation in the deinitializer.

However, I am not sure how to handle the (de)initialization of `Character`s inside the buffers.

- Since `Character` is a copyable value type which (funnily) is backed by a `String` which itself may reference memory managed by ARC, can blindly copying around `Character` instances to manually allocated buffers bypass ARC and cause memory leaks?
- Is it safe to just write `buffer[i] = character`, or do I need to explicitly call `initialize(to:)` and `deinitialize(count:)` when assigning or replacing elements?
- If manual (de)initialization is necessary, what’s the correct way to manage a buffer of `Character`s, especially when replacing or removing elements, or using them as a parameter / return value of a function

I would be grateful if anyone could help me understand proper memory management in Swift. Things seem a little less "easy" than in C (although its also easier there to mess up badly :P).

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [July 30, 2025, 1:02pm UTC](https://forums.swift.org/t/de-initialization-of-character-inside-of-unsafemutablebufferpointer/81398/2 "2025-07-30T13:02:59Z")

</div>

You should definitely just use an `Array` if you can. But, since you said this is a thought exercise for memory management...

> [@hex-ley](#):
>
> Is it safe to just write `buffer[i] = character`, or do I need to explicitly call `initialize(to:)` and `deinitialize(count:)` when assigning or replacing elements?

`buffer[i] = character` is fine **if** the elements in `buffer` have already been initialized, because setting an element via the subscript is equivalent to deinitializing what is currently there (releasing it) and then initializing the element in the buffer with the new value (copying it in).

If the elements in `buffer` haven't been initialized yet, then it would be an error to use the subscript because it would attempt to release an element that wasn't initialized. The [documentation for `subscript`](https://developer.apple.com/documentation/swift/unsafemutablebufferpointer/subscript(_:)-2vl82) mentions this:

> Uninitialized memory cannot be initialized to a nontrivial type using this subscript. Instead, use an initializing method, such as `initializeElement(at:to:)`.

In general, if you're managing a manual buffer of `Character`s (or any non-trivial/non-POD value), you would need to either

- Initialize every element in the buffer to some default value when you allocate it and ensure that elements are always initialized, or
- Leave unused elements uninitialized, and track in some sidecar storage which elements are initialized and which aren't.

Because, before you deallocate the buffer, you need to manually deinitialize all the elements in it, either by calling `deinitialize()` (this assumes they're all initialized), or looping through and calling `deinitializeElement(at:)` (which would let you limit yourself to only the elements that are initialized, assuming you're tracking it separately).

As far as passing the values around or returning them, you shouldn't need to worry about it once you're talking about a `Character` value instead of something inside the buffer. Reading an element from the buffer gives you a notional copy of it that you can work with independently, and writing an element into the buffer copies it in so that the original value going out of scope doesn't impact the one in the buffer.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [July 30, 2025, 1:15pm UTC](https://forums.swift.org/t/de-initialization-of-character-inside-of-unsafemutablebufferpointer/81398/3 "2025-07-30T13:15:18Z")

</div>

> [@hex-ley](#):
>
> I'm currently implementing a custom data structure based on two buffers, where each holds the characters left and right to the cursor respectively. Sure I could just use `[Character]` arrays instead. But since I also know C I wanted to learn how to manually manage memory in Swift, and hopefully squeeze out some extra performance.

Do you need O(1) random access into either buffer? If not, it would be much simpler to allocate a pair of UInt8 buffers and store UTF8 encoded text there instead.

Also you might find this relevant: [Gap buffer - Wikipedia](https://en.m.wikipedia.org/wiki/Gap_buffer).

---

<div class="post-metadata">

**Author:** ![hex-ley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hex-ley/32/42336_2.png) [@hex-ley](https://forums.swift.org/u/hex-ley)\
**Post date:** [July 30, 2025, 1:50pm UTC](https://forums.swift.org/t/de-initialization-of-character-inside-of-unsafemutablebufferpointer/81398/4 "2025-07-30T13:50:16Z")

</div>

@allevato Thanks, this really helps a lot!

@Slava_Pestov The problem with UTF8 is decoding and traversing it. While doing that is relatively easy for single codepoints, I have yet to learn about how to deal with characters consisting of more than one. I'll think about it though.  
Thanks for the article about gap buffers though! Their resizing doesn't seem too difficult to implement so I may switch over to one. Might result in better cache locality or something :P
