# Ref / Pointer Usage

**URL:** <https://discourse.julialang.org/t/ref-pointer-usage/8569>\
**Category:** General Usage\
**Created:** [January 24, 2018, 7:38pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569 "2018-01-24T19:38:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [January 24, 2018, 7:38pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/1 "2018-01-24T19:38:20Z")

</div>

Hi,

I would like to do the following

```julia
julia> type ZInt
           aint::Int
       end

julia> z = ZInt(4)
ZInt(4)

julia> ref = Ref(z.aint)
Base.RefValue{Int64}(4)

julia> ref.x = 3
3

julia> z
ZInt(4) 

```

I want the z to evaluate to ZInt(3)  
How can i get this done?  
Thanks

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 24, 2018, 8:08pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/2 "2018-01-24T20:08:32Z")

</div>

`z[] = 3`

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [January 24, 2018, 8:13pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/3 "2018-01-24T20:13:45Z")

</div>

I want to edit the content of z using ref in this case.  
Editing must be via ref like a pointer and as a result the z 's content will be changed.!

Thanks

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 24, 2018, 9:15pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/4 "2018-01-24T21:15:41Z")

</div>

The only sensible thing you can do is

```julia
ref = z
ref.aint=3
z
ZInt(3)

```

Obviously you could play pointer games. But ultimately, you want your ref to keep z alive, and the way to do this is ref=z. This emits mostly the same code as

```julia
ref=reinterpret(Ptr{Int},pointer_from_objref(z))
unsafe_store!(ref, 5)
z
ZInt(5)

```

It’s just that you don’t have to calculate / look up field offsets and the garbage collector keeps you safe. Afaik there is no advantage of using references; sometimes you might need pointers for performance if you want to store them in a bitstype, but this is always slightly ugly.

---

<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:** [January 24, 2018, 9:26pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/5 "2018-01-24T21:26:29Z")

</div>

Taking references to mutable fields is part of [WIP: Make mutating immutables easier by Keno · Pull Request #21912 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/21912), but we don’t have that feature yet.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 24, 2018, 10:09pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/6 "2018-01-24T22:09:13Z")

</div>

Just asking for interest… if

```julia
mutable struct foo
x::Int64 
y::Int64 
end 
f=foo(1,2)

```

and I take a reference to `f.y` which has type `Ref{Int64}`, how will gc find out that the allocation it needs to keep alive starts 8 bytes prior? (ok, in reality it starts even earlier because of the typetag, but for gc purposes it needs to subtract 8)

---

<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:** [January 24, 2018, 10:30pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/7 "2018-01-24T22:30:10Z")

</div>

`Ref` is not a concrete type. The field reference type will have all the information necessary.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 24, 2018, 10:40pm UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/8 "2018-01-24T22:40:20Z")

</div>

So this would then be mostly code\_native-equivalent to the following? (with the same limitations regarding storage as all non-bitstypes)

```julia
immutable foo_y_ref
parent::foo
end
Base.getindex(foo_y_ref) = parent.y
Base.setindex!(foo_y_ref, val) = (parent.y=val)
Base.pointer(foo_y_refl) = reinterpret(Ptr{Int64}, pointer_from_objref(parent)+8)

```

That is, the ref would be pointer-sized, and a type encoding offset would be created?

edit: and the concrete type would rather be something like `Ref{foo, 2}` because we refer to the second field of foo, instead of `Ref{Int64, plain}`? And maybe something something covariance this should become a subtype of an abstract `Ref{Int64, abstract}`?

---

<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:** [January 25, 2018, 12:42am UTC](https://discourse.julialang.org/t/ref-pointer-usage/8569/9 "2018-01-25T00:42:42Z")

</div>

> [@foobar\_lv2](#):
>
> (with the same limitations regarding storage as all non-bitstypes)

Yes.

> [@foobar\_lv2](#):
>
> a type encoding offset would be created?

Ideally not. There’s basically no point to store it in the type.

> [@foobar\_lv2](#):
>
> he concrete type would rather be something like Ref{foo, 2}

No.
