# Is this conversion+store safe?

**URL:** https://discourse.julialang.org/t/is-this-conversion-store-safe/133533
**Category:** Performance
**Tags:** question
**Created:** [October 30, 2025, 2:07am UTC](https://discourse.julialang.org/t/is-this-conversion-store-safe/133533 "2025-10-30T02:07:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)
#### Post date: [October 30, 2025, 2:07am UTC](https://discourse.julialang.org/t/is-this-conversion-store-safe/133533/1 "2025-10-30T02:07:49Z")

</div>

I’m wondering if this is a correct usage of these unsafe functions:

```julia-auto
julia> using StaticArrays

julia> a = MVector{4,UInt}(1,2,3,4);

julia> b = UInt.((5,6,7,8));

julia> function f(a, b)
           dst = Base.unsafe_convert(Ptr{MVector{4, UInt64}}, a)
           unsafe_store!(dst, b)
           return a
       end

julia> f(a, b)
4-element MVector{4, UInt64} with indices SOneTo(4):
 0x0000000000000005
 0x0000000000000006
 0x0000000000000007
 0x0000000000000008

```

is it?

Should I use instead

```julia-auto
julia> function f(a, b)
           GC.@preserve a begin
               dst = Base.unsafe_convert(Ptr{MVector{4, UInt64}}, a)
               unsafe_store!(dst, b)
           end
           return a
       end

```

maybe?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 30, 2025, 5:37am UTC](https://discourse.julialang.org/t/is-this-conversion-store-safe/133533/2 "2025-10-30T05:37:06Z")

</div>

Since this is an `MVector`, why use unsafe functions at all?

---

<div class="post-metadata">

### Author: ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)
#### Post date: [October 30, 2025, 5:39am UTC](https://discourse.julialang.org/t/is-this-conversion-store-safe/133533/3 "2025-10-30T05:39:41Z")

</div>

> [@Tortar](#):
>
> is it?

No, it’ll crash

> [@Tortar](#):
>
> ```julia-auto
> julia> function f(a, b)
> GC.@preserve a begin
> dst = Base.unsafe_convert(Ptr{MVector{4, UInt64}}, a)
> unsafe_store!(dst, b)
> end
> return a
> end
> 
> ```

Certainly better, although these things can be very tricky and I don’t think we’re guaranteeing the memory layout here. If at all possible, try to use the APIs provided by the package (which may do similar things, but will likely get caught in PkgEval if we break an implicit assumption). If the issue is performance, file an issue.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [October 30, 2025, 9:28am UTC](https://discourse.julialang.org/t/is-this-conversion-store-safe/133533/4 "2025-10-30T09:28:23Z")

</div>

> [@Sukera](#):
>
> Since this is an `MVector`, why use unsafe functions at all?

To expand on this, you can just use `a.data = b` here:

```julia-auto
julia> using StaticArrays

julia> a = MVector{4, UInt}(1,2,3,4);

julia> b = UInt.((5,6,7,8));

julia> a.data = b;

julia> a
4-element MVector{4, UInt64} with indices SOneTo(4):
 0x0000000000000005
 0x0000000000000006
 0x0000000000000007
 0x0000000000000008

```

* * *

If you’re exploring unsafe pointer shenanigans though, it’s perhaps also worth noting if you’re going down this path that you can’t `unsafe_load` a `Ptr{T}` when `T` is a mutable type (like `MVector`), since the GC will incorrectly reason about the loaded object.

---

<div class="post-metadata">

### Author: ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)
#### Post date: [October 30, 2025, 12:52pm UTC](https://discourse.julialang.org/t/is-this-conversion-store-safe/133533/5 "2025-10-30T12:52:11Z")

</div>

thank you all! Indeed, I can use the straightforward version of @Mason, I thought the only other option was `a[1]=b[1], a[2]=b[2], ...`
