# Does this still crash on new Julia versions?

**URL:** <https://discourse.julialang.org/t/does-this-still-crash-on-new-julia-versions/118945>\
**Category:** General Usage\
**Created:** [September 2, 2024, 12:56pm UTC](https://discourse.julialang.org/t/does-this-still-crash-on-new-julia-versions/118945 "2024-09-02T12:56:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [September 2, 2024, 12:56pm UTC](https://discourse.julialang.org/t/does-this-still-crash-on-new-julia-versions/118945/1 "2024-09-02T12:56:48Z")

</div>

```julia
#GC.enable(false) 
a = [1,2,3,4,5]
b = [Ptr{Int64}(i) for i in a]
#a = nothing
c = Base.unsafe_convert(Ptr{Ref{Int}},b)
d = unsafe_wrap(Array,c,5)
#Even with GC disabled, the crash still exists.

```

It asked me to file a bug report. However, my Julia version is not the most recent so it might be fixed in modern versions.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 2, 2024, 1:02pm UTC](https://discourse.julialang.org/t/does-this-still-crash-on-new-julia-versions/118945/2 "2024-09-02T13:02:19Z")

</div>

It’s not a bug — you can easily crash Julia by doing invalid operations with unsafe pointers. e.g. `unsafe_load(Ptr{Int}(0))` also crashes.

In your example, you are taking e.g. the first element `1` of `a`, and calling `Ptr{Int64}(1)` for the first element of `b`, whcih is already a pointer to an invalid memory address `1`. Then you are trying to reinterpret `b` as an array of pointers to `Ref{Int}` (an abstract type), which is also wrong.

Pointer operations are crucial to inter-operate with C libraries, for example. But they are marked “unsafe” for a reason — you have to know what you are doing, and use them in sensible ways.

---

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [September 2, 2024, 1:05pm UTC](https://discourse.julialang.org/t/does-this-still-crash-on-new-julia-versions/118945/3 "2024-09-02T13:05:44Z")

</div>

Oh, thank you. I thought Ptr{Int}(1) would be a pointer to 1. I was playing with some cursed code.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 2, 2024, 1:19pm UTC](https://discourse.julialang.org/t/does-this-still-crash-on-new-julia-versions/118945/4 "2024-09-02T13:19:39Z")

</div>

To get a valid pointer to an element of `a` you can use the `pointer` function:

```julia
p = pointer(a, 1)
unsafe_load(p)

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 2, 2024, 2:11pm UTC](https://discourse.julialang.org/t/does-this-still-crash-on-new-julia-versions/118945/5 "2024-09-02T14:11:39Z")

</div>

Note that `pointer` doesn’t do bounds checks, so you can ask for an element past the end of the intended array:

```julia
julia> unsafe_load(pointer([1, 2, 3], 4))
136772842288784

```
