# Trying to understand pointers and aliasing

**URL:** <https://discourse.julialang.org/t/trying-to-understand-pointers-and-aliasing/107604>\
**Category:** New to Julia\
**Created:** [December 14, 2023, 11:32am UTC](https://discourse.julialang.org/t/trying-to-understand-pointers-and-aliasing/107604 "2023-12-14T11:32:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 14, 2023, 11:32am UTC](https://discourse.julialang.org/t/trying-to-understand-pointers-and-aliasing/107604/1 "2023-12-14T11:32:58Z")

</div>

Hi there, I’m trying to understand what the difference is when you copy elements of an array to another array, vs. setting one equal to another (which results in both just being pointers to the same piece of memory, if I’m not mistaken?).

For example,

```julia
B = [1,2,3,4,5];
A = [10,11,12,13,14];
for i in 1:5
    B[i] = A[i]
end;
A[4] = 666;
B

```

gives the output

```julia
5-element Vector{Int64}:
 10
 11
 12
 13
 14

```

This makes sense to me, as I believe the loop is taking each element `i` of the vector `A` and copying it to `B`. Hence, when `A` is altered, `B` remains the same since it has copies of the old `A` entries instead of just pointers to the memory in `A`. Am I right in thinking this?

If so, then I can’t figure out what’s wrong with this code I’m working on, which has the following snippet:

```julia
for i in 1:length(phi)
    phi_matrix[n_step][i] = phi[i]
end

```

where `phi_matrix` is just a vector of vectors, each of the individual vectors being copies of `phi` at different timesteps `n_step`. Now, the code does what it’s meant to do when I write `phi_matrix[n_step] = copy(phi)` instead of the loop. This makes sense, but I’m allocating more memory to creating a copy of `phi`, it seems. However, if I use the loop above, every element of the `phi_matrix` seems to just be a pointer to whatever phi is, since at the end of the simulation every vector in `phi_matrix` appears to be a copy of the current `phi`. So why does the snippet above exhibit this aliasing effect that I would have thought doesn’t happen in loops, given the example I gave at the beginning of this question? If nobody thinks this should happen, then I’ll have a look at the rest of my code and see if something else is causing this problem.

Thank you!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 14, 2023, 11:47am UTC](https://discourse.julialang.org/t/trying-to-understand-pointers-and-aliasing/107604/2 "2023-12-14T11:47:25Z")

</div>

> [@LucasMontoya4](#):
>
> Am I right in thinking this?

Yes

> [@LucasMontoya4](#):
>
> where `phi_matrix` is just a vector of vectors, each of the individual vectors being copies of `phi` at different timesteps `n_step`.

This is because in your first example the elements of the vectors are immutable, thus sharing occurs by copying the values. In the second case your elements are mutable, and sharing occurs by copying the reference. When you do:

```julia-repl
julia> x = [[1,2] ]
1-element Vector{Vector{Int64}}:
 [1, 2]

julia> y = [[3, 4] ]
1-element Vector{Vector{Int64}}:
 [3, 4]

julia> y[1] = x[1]
2-element Vector{Int64}:
 1
 2

julia> y[1] === x[1]
true

```

What is copied from `x[1]` to `y[1]` is the reference to the vector in `x[1]`, not the vector itself.

---

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 14, 2023, 11:55am UTC](https://discourse.julialang.org/t/trying-to-understand-pointers-and-aliasing/107604/3 "2023-12-14T11:55:58Z")

</div>

Ohhhhh, I see. So scalar elements within a vector are immutable, but vectors themselves are mutable?

Also, the element `phi[i]` that I’m trying to assign to the `phi_matrix[n_step]` vector is itself just an element, rather than a vector. I’m not trying to assign a vector to another, right? So why does your argument apply?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 14, 2023, 12:00pm UTC](https://discourse.julialang.org/t/trying-to-understand-pointers-and-aliasing/107604/4 "2023-12-14T12:00:11Z")

</div>

> [@LucasMontoya4](#):
>
> Ohhhhh, I see. So scalar elements within a vector are immutable, but vectors themselves are mutable?

Yes

> [@LucasMontoya4](#):
>
> Also, the element `phi[i]` that I’m trying to assign to the `phi_matrix[n_step]` vector is itself just an element, rather than a vector.

Than it should work exactly as in your first example. If it is not, the problem is somewhere else (and the argument _doest not_ apply).

---

<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:** [December 14, 2023, 1:36pm UTC](https://discourse.julialang.org/t/trying-to-understand-pointers-and-aliasing/107604/5 "2023-12-14T13:36:46Z")

</div>

> [@LucasMontoya4](#):
>
> Hi there, I’m trying to understand what the difference is when you copy elements of an array to another array, vs. setting one equal to another (which results in both just being pointers to the same piece of memory, if I’m not mistaken?).

See also the [assignment vs. mutation](https://docs.julialang.org/en/v1/manual/variables/#man-assignment-expressions) section of the manual.

---

<div class="post-metadata">

**Author:** ![LucasMontoya4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmontoya4/32/205442_2.png) [@LucasMontoya4](https://discourse.julialang.org/u/LucasMontoya4)\
**Post date:** [December 15, 2023, 1:11pm UTC](https://discourse.julialang.org/t/trying-to-understand-pointers-and-aliasing/107604/6 "2023-12-15T13:11:53Z")

</div>

Thank you both. I’ll have a look…
