# Pointer arithmetic with Builtin.RawPointer?

**URL:** <https://forums.swift.org/t/pointer-arithmetic-with-builtin-rawpointer/20322>\
**Category:** Using Swift\
**Created:** [February 10, 2019, 5:00pm UTC](https://forums.swift.org/t/pointer-arithmetic-with-builtin-rawpointer/20322 "2019-02-10T17:00:30Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [February 10, 2019, 11:48pm UTC](https://forums.swift.org/t/pointer-arithmetic-with-builtin-rawpointer/20322/9 "2019-02-10T23:48:14Z")

</div>

> [@ignat](#):
>
> The only final thoughts I would have would be that `load(as T.self)` on an `UnsafeRawPointer` instance creates a new instance of that type, so if I understand correctly it won't use the same memory space where the UnsafeRawPointer points to. If I am sure that the memory is bound correctly already, it would be fine to use `.assumingMemoryBound(as: T.Type)` instead, right?

Yes, the documentation for `load` states that it will copy the memory to initialise a new, independent T.

As for raw/typed pointer, unfortunately we don't really have great high-level documentation about it. The [official language guide](https://docs.swift.org/swift-book/LanguageGuide/MemorySafety.html) doesn't mention the distinction at all, and I couldn't find any design documents in the compiler source repository. If you go back to [the Swift 3 migration notes](https://swift.org/migration-guide-swift3/se-0107-migrate.html) though, it does say this is okay:

> In general, developer’s should not make layout assumptions. However, some “obvious” cases can be safely assumed, including homogeneous arrays and tuples, and structs with homogeneous stored properties. _Imported C structs naturally follow the layout rules of the platform’s C ABI._

As you've seen though, the interface is more awkward once you need to add byte-offsets and read values of different types. This kind of parsing of binary layouts is [largely why the raw APIs exist](https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md#raw-buffer-of-unknown-type).

Also check out @Andrew_Trick's [recent post](https://forums.swift.org/t/unsafe-functions/20137/4) on correct use of these APIs.

* * *

> [@ignat](#):
>
> Is there a better way to get the segname out of the pointer? It is defined in c as `char segname[16];` but is imported into Swift as a tuplet of 16 `Int8` 's. I don't know a `String` constructor that would take that tuplet directly.

You can use `withUnsafe(Mutable)Pointer(to: inout T)` on the tuple, transform it in to a fixed-capacity buffer [as shown here](https://github.com/apple/swift/blob/f19aca6cb0c8ea8f392a78a56136151b25f8713e/stdlib/public/core/UnicodeScalarProperties.swift#L783), then use `String.init(cString:)`.

---

_[View the full topic](https://forums.swift.org/t/pointer-arithmetic-with-builtin-rawpointer/20322)._
