# How to create a static/constant \`UnsafePointer\`?

**URL:** <https://forums.swift.org/t/how-to-create-a-static-constant-unsafepointer/71687>\
**Category:** Using Swift\
**Created:** [May 8, 2024, 1:01am UTC](https://forums.swift.org/t/how-to-create-a-static-constant-unsafepointer/71687 "2024-05-08T01:01:03Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [May 8, 2024, 4:50pm UTC](https://forums.swift.org/t/how-to-create-a-static-constant-unsafepointer/71687/14 "2024-05-08T16:50:14Z")

</div>

> [@SlugFiller](#):
>
> I wasn't looking for a global mutable static state. If anything, immutable is preferred.
> 
> Can you explain the array method in more detail? If I do `[UInt8]([1, 2, 3]).withUnsafeBufferPointer`, and just return the result, is this considered "safe", and is the compiler likely to optimize away the function call into a compile-time constant?

Strictly speaking, it's against the rules, but there's no practical way it could become unsafe in practice. This would be formalized using [nonescaping types](https://forums.swift.org/t/pitch-non-escapable-types-and-lifetime-dependency/69865) where a value dependent on an immortal value is itself immortal. And it looks like it does compile down to a global array constant and a direct pointer access: [Compiler Explorer](https://swift.godbolt.org/z/65sqfxh9E)

---

_[View the full topic](https://forums.swift.org/t/how-to-create-a-static-constant-unsafepointer/71687)._
