# Create a nonnull UnsafeRawPointer of underlying rawValue 0

**URL:** <https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225>\
**Category:** Using Swift\
**Tags:** pointers\
**Created:** [October 8, 2024, 12:36pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225 "2024-10-08T12:36:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [October 8, 2024, 12:36pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/1 "2024-10-08T12:36:01Z")

</div>

## Issue

If we use `.init(bitPattern:)` and pass 0 here. We'll get a nil value.

An alternative way is use `unsafeBitCast(0, to: UnsafeRawPointer)`.

Unfortunately, a warning will be emitted and I'm use `werror` here.

```swift
'unsafeBitCast' from 'Int' to 'UnsafeRawPointer' can be replaced with 'bitPattern:' initializer on 'UnsafeRawPointer'

```

> They are actually not interchangeable. Since `.init(bitPattern:)` will return nil for 0.

Is there any other solutions or how can I disable the warning emitted by compiler here.

## Context:

I'd like the pointer to be invalid at first and assign it to be valid later. But I do not want to use `UnsafeRawPointer!` here

---

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [October 8, 2024, 12:40pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/2 "2024-10-08T12:40:08Z")

</div>

Another solution I can think of is by Pointer calculation. But it is less elegant than `unsafeBitCast`.

eg.

```swift
let a = 1 // "a" can be any number except 0
let b = UnsafeRawPointer(bitPattern: a)! - a

```

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [October 8, 2024, 12:45pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/3 "2024-10-08T12:45:03Z")

</div>

> [@Kyle-Ye](#):
>
> I'd like the pointer to be invalid at first and assign it to be valid later. But I do not want to use `UnsafeRawPointer!` here

Why not? This sounds like exactly what that type is for.

---

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [October 8, 2024, 1:08pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/4 "2024-10-08T13:08:25Z")

</div>

I know the best practices and the consequence here.

But for some special low-level purpose, I need the feature and is trying to make compiler happy.

Here is the final answer I found in the end - use unsafeBitCast without the annoying warning.

```swift
unsafeBitCast(Double.zero, to: UnsafeRawPointer.self)
// Or any other 64-bit and zeroable struct
// eg. struct A { let value: Int }

```

---

<div class="post-metadata">

**Author:** ![CrystDragon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crystdragon/32/10461_2.png) [@CrystDragon](https://forums.swift.org/u/CrystDragon)\
**Post date:** [October 8, 2024, 1:14pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/5 "2024-10-08T13:14:37Z")

</div>

I think Swift choose this behavior of `UnsafeRawPointer.init` for a reason. A pointer with an address of 0 is invalid on almost every machine. So maybe it can help to prevent you from accidentally forgetting to re-assign that pointer.

Out of curiosity, why not just use `UnsafeRawPointer?`, it's just as efficient as plain `UnsafeRawPointer` in terms of memory usage.

---

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [October 8, 2024, 1:49pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/6 "2024-10-08T13:49:15Z")

</div>

> [@CrystDragon](#):
>
> Out of curiosity, why not just use `UnsafeRawPointer?`, it's just as efficient as plain `UnsafeRawPointer` in terms of memory usage.

Just to avoid ? and seek for absolute performance here.

Again I know the best practices and the consequence. Just try to workaround the compiler limitation/warning.

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [October 8, 2024, 1:54pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/7 "2024-10-08T13:54:02Z")

</div>

As a general rule, there is no performance overhead to `UnsafeRawPointer?` vs. `UnsafeRawPointer`. `UnsafeRawPointer?` has exactly the same in-memory layout as `UnsafeRawPointer`, and the `.none` case (`nil`) is simply represented as `0x0` in memory—that is, it is exactly equivalent to C's null pointer.

The only time you might see any sort of runtime overhead (ignoring generics where things might not be fully specialized) would be when testing for `nil`, but the overhead there is again _exactly_ the same as the overhead you'd have checking for `NULL` in C.

(Giant asterisk: when building with optimizations off, you will likely see additional runtime overhead, but that's just what "optimizations off" means and the same can be said of equivalent C code.)

---

<div class="post-metadata">

**Author:** ![dmt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmt/32/30830_2.png) [@dmt](https://forums.swift.org/u/dmt)\
**Post date:** [October 8, 2024, 2:05pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/8 "2024-10-08T14:05:52Z")

</div>

When building with optimizations there still will be a test instruction that branches to a trap when the pointer is nil. The `unsafelyUnwrapped` property is a way to unwrap it without any checks at all (in -O builds).

---

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [October 8, 2024, 2:42pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/9 "2024-10-08T14:42:05Z")

</div>

Thanks for the clarification. I'll use `UnsafeRawPointer?` then.

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [October 8, 2024, 3:12pm UTC](https://forums.swift.org/t/create-a-nonnull-unsaferawpointer-of-underlying-rawvalue-0/75225/10 "2024-10-08T15:12:29Z")

</div>

That trap is only relevant when force-unwrapping the optional value. If you don't force-unwrap it, the trap instruction shouldn't be emitted.
