# Copying subvector makes pointer?

**URL:** <https://discourse.julialang.org/t/copying-subvector-makes-pointer/56935>\
**Category:** New to Julia\
**Created:** [March 11, 2021, 9:01am UTC](https://discourse.julialang.org/t/copying-subvector-makes-pointer/56935 "2021-03-11T09:01:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [March 11, 2021, 9:01am UTC](https://discourse.julialang.org/t/copying-subvector-makes-pointer/56935/1 "2021-03-11T09:01:44Z")

</div>

Hi!  
I’m getting some surprising behavior from a Vector of Vectors.

```julia
julia> b=[[1,2,3]]
1-element Array{Array{Int64,1},1}:
 [1, 2, 3]

julia> pushfirst!(b,b[1])
2-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [1, 2, 3]

julia> b[1][1]=4
4

julia> b
2-element Array{Array{Int64,1},1}:
 [4, 2, 3]
 [4, 2, 3]

```

Why does this happen? This doesn’t happen if I have an array (instead of array of arrays).  
I can solve it by using `pushfirst!(b,copy(b[1]))`, but that seems clunky.  
I found many tutorials online that state that `a=b` is a pointer and modifying `b` will modify `a`, but that is not the case. Perhaps it was for older versions? Then maybe this is a lingering version of that?  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 11, 2021, 9:09am UTC](https://discourse.julialang.org/t/copying-subvector-makes-pointer/56935/2 "2021-03-11T09:09:10Z")

</div>

> [@Ribeiro](#):
>
> I found many tutorials online that state that `a=b` is a pointer and modifying `b` will modify `a` , but that is not the case.

It’s still the case:

```julia
julia> x = [1,2,3];

julia> y = x;

julia> x[1] = 42;

julia> y
3-element Array{Int64,1}:
 42
  2
  3

```

To your original question, scalar indexing, i.e. `b[1]`, (in contrast to slicing, i.e. `b[1:2]`) generally doesn’t create a copy. That’s why you see the behaviour you see. The “it doesn’t happen if I don’t have an array of arrays” part is due to the fact that the elements `b[1]` in this case are immutable numbers (in contrast to mutable arrays). Compare this to the example above:

```julia
julia> x = 4;

julia> y = x;

julia> x = 42;

julia> y
4

```

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 11, 2021, 9:09am UTC](https://discourse.julialang.org/t/copying-subvector-makes-pointer/56935/3 "2021-03-11T09:09:20Z")

</div>

You’ve basically answered your own question. `b` is an array of pointers and you effectively put the same pointer in that array twice. No when you change the memory these pointers point to it affects them both. Copying the memory before you change it is the correct solution.

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [March 11, 2021, 9:29am UTC](https://discourse.julialang.org/t/copying-subvector-makes-pointer/56935/4 "2021-03-11T09:29:08Z")

</div>

I see. So Julia is like C, and arrays are always pointers, while scalars behave more normally.  
Good to know! Thanks for clarifying!

---

<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:** [March 11, 2021, 10:06am UTC](https://discourse.julialang.org/t/copying-subvector-makes-pointer/56935/5 "2021-03-11T10:06:59Z")

</div>

> [@Ribeiro](#):
>
> behave more normally.

This extract from previous threads helped me (related, but not exactly the same thing): [Assignment and mutation · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/assignment/)

---

<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:** [March 11, 2021, 8:58pm UTC](https://discourse.julialang.org/t/copying-subvector-makes-pointer/56935/6 "2021-03-11T20:58:02Z")

</div>

They’re not necessarily always pointers - that’s an implementation detail. Types that are `isbits` for example may be stored inline. Mutability semantics are detached from that notion.

It’s not generally useful to fit pointer logic into julia semantics.
