# Pointers in Julia documentation

**URL:** <https://discourse.julialang.org/t/pointers-in-julia-documentation/119571>\
**Category:** General Usage\
**Tags:** question, memory\
**Created:** [September 18, 2024, 8:19pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571 "2024-09-18T20:19:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [September 18, 2024, 8:19pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/1 "2024-09-18T20:19:15Z")

</div>

Hi, I was trying to implement some algorithms and data structures such as linked list, for which I need pointers. I wanted to know why is the use of pointers so obscured. I have not really found any formal explanation of use and limits of them in the documentation. Some spread answers here and there in forums, but I really want to know, can I make a fully working code for linked lists, graphs etc dealing with pointers.

To make it explicit:  
In C++ I can have an object Node that stores it’s element and the pointer to the next node:

```julia
template <typename T>
class Node{
    Node* next:
    T elem
    //constructor...
}

```

But in Julia, without pointers, I would have to make

```julia
mutable struct Node{T}
    el::T
    prev::Union{Node{T},Nothing}
    next::Union{Node{T},Nothing}
end

```

But that, to my understanding, would be less efficient in memory than a pointer, because it would need for the node object to contain the whole two related node objects (one object if is a single linked list), instead of just having a pointer like field.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 18, 2024, 8:36pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/2 "2024-09-18T20:36:42Z")

</div>

You don’t want pointers. You need to understand [pass by sharing](https://docs.julialang.org/en/v1/manual/functions/#man-argument-passing). For an example linked list implimentation, see [DataStructures.jl/src/list.jl at master · JuliaCollections/DataStructures.jl · GitHub](https://github.com/JuliaCollections/DataStructures.jl/blob/master/src/list.jl)

---

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [September 18, 2024, 8:53pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/3 "2024-09-18T20:53:06Z")

</div>

I edited my question to be clearer. I would need pointers beyond the passing by reference aspect of them

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 18, 2024, 9:06pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/4 "2024-09-18T21:06:47Z")

</div>

one option is

```julia
mutable struct Node{T}
    el::T
    prev::Node{T}
    next::Node{T}
end

```

and make `prev`/`next` be circular if there is no next/prev element.

---

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [September 18, 2024, 9:08pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/5 "2024-09-18T21:08:59Z")

</div>

But my point is that by having the field prev as type Node{T} I am not being efficient in memory. Every Node will have itself another object Node as its field. In the c++ code it just has a reference, if the type T is memory consuming, the longer the list, the more I will be “duplicating” memory and being inefficient, am I wrong?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 18, 2024, 9:10pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/6 "2024-09-18T21:10:27Z")

</div>

these `Node{T}`s are just references.

```julia
mutable struct Node{T}
    el::T
    prev::Node{T}
    next::Node{T}
    function Node(el::T) where T
        N = new{T}(el)
        N.prev = N
        N.next = N
    end
end
julia> dump(Node(5))
Node{Int64}
  el: Int64 5
  prev: Node{Int64}#= circular reference @-1 =#
  next: Node{Int64}#= circular reference @-1 =#

```

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [September 18, 2024, 11:47pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/7 "2024-09-18T23:47:16Z")

</div>

> [@martin\_sanchez](#):
>
> if the type T is memory consuming, the longer the list, the more I will be “duplicating” memory and being inefficient, am I wrong?

Yes. Consider

```julia
struct Foo # or mutable struct, doesn't matter
    x::Bar
end

```

If `Bar` is a mutable type, an instance of `Foo` will only contain a reference, not an inlined value. This is necessary to preserve the semantics of mutable memory: If I stick the same instance of `Bar` into several different instances of `Foo` and mutate it via one of them, the change should be observed in all of them. This is only possible if all the `Foo`s contain a reference to the same `Bar`, rather than separate copies of the `Bar` value.

Since your `Node` is mutable, it will always be passed around by reference and never duplicated, whether inside other `Node`s or anywhere else in your program.

Thus you can often get around without explicitly worrying about pointers in Julia. For the few occasions where you do, there’s a dedicated wrapper called `Ref`.

---

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [September 19, 2024, 1:28pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/8 "2024-09-19T13:28:54Z")

</div>

> [@danielwe](#):
>
> often get around without explicitly worrying about pointers in Julia. For the few occasions where you do, there’s a dedicated wrapper called

Thank you. One doubt: if Bar is just a struct, not mutable, then would it also contain just the reference? Where in the documentation can I see that

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 19, 2024, 2:18pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/9 "2024-09-19T14:18:30Z")

</div>

If `Bar` isn’t mutable then the compiler is allowed to make either choice (but usually will store it inline). The fundamental difference between mutables and immutables is that mutables have reference identity while immutables have value identity. Since you can’t change the value of an immutable, your program can’t find a difference between different “copies” of an immutable with the same value.

---

<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:** [September 19, 2024, 3:18pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/10 "2024-09-19T15:18:22Z")

</div>

I find myself only very rarely reaching for `Ref` (the julia-level equivalent to a pointer) and even more rarely for an actual `Ptr` (actual, C-like pointers). The latter is basically only relevant for FFI interop.

If you’re worried about memory consumption of your `Node` struct, try one using `Ref` to refer to its neighbors and compare its `sizeof` with the version of the struct where you place the neighbors “directly inside” the struct. They should be the same in the mutable case, due to the above mentioned pass-by-sharing.

---

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [October 29, 2025, 2:37pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/12 "2025-10-29T14:37:55Z")

</div>

Thank you. Could you give me a case where the use of Ref would be relevant, then? As I am understanding. Using the direct reference and using mutables would solve the issue. Why would you need Ref for?

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [October 29, 2025, 3:12pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/13 "2025-10-29T15:12:43Z")

</div>

You don’t need `Ref` for this case. What Sukera was illustrating with `Ref` is a _comparison tool_: using `sizeof` with and without `Ref` shows the difference between an inlined isbits field and a pointer-sized field. In a linked list, `Node`’s `next` is inherently a reference (non-isbits, recursive type), so `sizeof` already demonstrate that `next` is stored as a pointer rather than inlining another `Node`.

`fieldoffset` can also demonstrate this point:

```julia
# If T is isbits, `value` is inlined into the Node object itself.
# `next` is a recursive reference and is stored as a pointer (not inlined).
mutable struct Node{T}
    value::T
    next::Node{T}
end

T = Node{Int}
# On a 64-bit system, a typical layout is:
sizeof(T) # 16 (8 bytes for the inlined Int value + 8 bytes for the `next` pointer)

off1 = fieldoffset(T, 1) # offset of :value (e.g., 0x0000000000000000)
off2 = fieldoffset(T, 2) # offset of :next (e.g., 0x0000000000000008)

fieldsize_value = off2 - off1 # e.g., 0x0000000000000008 → 8 bytes for `value` (Int)
fieldsize_next = sizeof(T) - off2 # e.g., 0x0000000000000008 → 8 bytes for `next` (a pointer)
# `fieldsize_next` is precisely the storage size of the `next` field inside Node: one pointer width.

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [October 29, 2025, 3:23pm UTC](https://discourse.julialang.org/t/pointers-in-julia-documentation/119571/14 "2025-10-29T15:23:58Z")

</div>

> [@martin\_sanchez](#):
>
> Could you give me a case where the use of Ref would be relevant, then?

The documentation for Ref lists two specific use cases. One is to pass a pointer to `ccall`, and the other is to prevent broadcasting. [C Interface · The Julia Language](https://docs.julialang.org/en/v1/base/c/#Core.Ref)
