# When to use or not use \`reinterpret\`?

**URL:** <https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394>\
**Category:** New to Julia\
**Created:** [May 29, 2020, 4:21am UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394 "2020-05-29T04:21:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [May 29, 2020, 4:21am UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394/1 "2020-05-29T04:21:16Z")

</div>

I reinterpret an array of bytes (`UInt8`) as `Int64` like this

```julia
dict = reinterpret(Int64, uncompressed_data)

```

and when I use `dict` via a bunch of essentially random indices like so

```julia
for i in indices
  do_something(dict[i])
end

```

So I am accessing `dict[i]` like a random access array. Is this a bad way to use reinterpret? I think using `unsafe_wrap` to create the `dict` results in faster random access overall. Although the code is complex, so I need to write a simple MWE to confirm.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 29, 2020, 6:18am UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394/2 "2020-05-29T06:18:50Z")

</div>

… naming an array `dict` isn’t a particular good choice IMHO…

* * *

Anyway, if you get the data directly as pointer or read it from somewhere else, you should create the array with the right type from start.

If you already have the array in a normal `Array` the creating another array using `unsafe_wrap` is illegal. You must not pass any pointer from any julia objects to `unsafe_wrap`. Doing so can crash your program and you are just lucky that it didn’t.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [May 29, 2020, 6:22am UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394/3 "2020-05-29T06:22:02Z")

</div>

> [@yuyichao](#):
>
> `dict` isn’t a particular good choice IMHO

That’s a good point. Parquet calls the array a “dictionary” though, so it’s in relation to the parquet reader. Maybe I call it `vec_dict` or something.

> [@yuyichao](#):
>
> If you already have the array in a normal `Array` the creating another array using `unsafe_wrap` is illegal.

Awesome to know.

---

<div class="post-metadata">

**Author:** ![lmtzx9h4qqnt](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lmtzx9h4qqnt](https://discourse.julialang.org/u/lmtzx9h4qqnt)\
**Post date:** [October 4, 2024, 11:05am UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394/4 "2024-10-04T11:05:19Z")

</div>

> [@yuyichao](#):
>
> If you already have the array in a normal `Array` the creating another array using `unsafe_wrap` is illegal. You must not pass any pointer from any julia objects to `unsafe_wrap`. Doing so can crash your program and you are just lucky that it didn’t.

Sorry for digging out this old topic, but I just stumbled across this in another context. It seems that this is exactly what’s done in RealFFTs.jl:

> <https://github.com/JuliaMath/RealFFTs.jl/blob/24fae071fc77420f618fff14941671f751759726/src/RealFFTs.jl#L54-L55>

@yuyichao, does that mean that this shouldn’t be implemented in this way in RealFFTs.jl, or was that more of a simplified/exaggerated statement for newcomers?

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [October 4, 2024, 3:50pm UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394/5 "2024-10-04T15:50:53Z")

</div>

the same question, then what’s the correct usage of `unsafe_wrap`?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 4, 2024, 4:02pm UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394/6 "2024-10-04T16:02:36Z")

</div>

> [@lmtzx9h4qqnt](#):
>
> this shouldn’t be implemented in this way in [RealFFTs.jl](https://juliahub.com/ui/Packages/General/RealFFTs)

This. As documented.

> [@photor](#):
>
> the same question, then what’s the correct usage of `unsafe_wrap`?

For wrapping foreign, non-Julia memory. As discussed above.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [October 4, 2024, 4:29pm UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394/7 "2024-10-04T16:29:12Z")

</div>

And what’s the correct way to implement this, if `unsafe_wrap` cannot be used?

---

<div class="post-metadata">

**Author:** ![lmtzx9h4qqnt](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@lmtzx9h4qqnt](https://discourse.julialang.org/u/lmtzx9h4qqnt)\
**Post date:** [October 4, 2024, 4:33pm UTC](https://discourse.julialang.org/t/when-to-use-or-not-use-reinterpret/40394/8 "2024-10-04T16:33:49Z")

</div>

> [@nsajko](#):
>
> As documented.

Where?

> ```plaintext
> help?> unsafe_wrap
> search: unsafe_wrap unsafe_write unsafe_swap!
> 
> unsafe_wrap(Array, pointer::Ptr{T}, dims; own = false)
> 
> Wrap a Julia Array object around the data at the address given by pointer, without making a copy. The pointer element type T determines
> the array element type. dims is either an integer (for a 1d array) or a tuple of the array dimensions. own optionally specifies whether
> Julia should take ownership of the memory, calling free on the pointer when the array is no longer referenced.
> 
> This function is labeled "unsafe" because it will crash if pointer is not a valid memory address to data of the requested length. Unlike
> unsafe_load and unsafe_store!, the programmer is responsible also for ensuring that the underlying data is not accessed through two arrays
> of different element type, similar to the strict aliasing rule in C.
> 
> ```
