# Copying cross-referenced values

**URL:** <https://discourse.julialang.org/t/copying-cross-referenced-values/61442>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 19, 2021, 1:27pm UTC](https://discourse.julialang.org/t/copying-cross-referenced-values/61442 "2021-05-19T13:27:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [May 19, 2021, 1:27pm UTC](https://discourse.julialang.org/t/copying-cross-referenced-values/61442/1 "2021-05-19T13:27:06Z")

</div>

Hi,  
I have a rather newbie question. If I enter this code:

```julia
vecvec = [[5]]
doubled = [vecvec;vecvec]
copied = deepcopy(doubled)
println( "doubled: ", doubled)
println( "copied: ", copied)
copied[1][1] = 7
println( "doubled after change: ", doubled)
println( "copied after change: ", copied)

```

then I get this output:

```julia
doubled: [[5], [5]]
copied: [[5], [5]] 
doubled after change: [[5], [5]]
copied after change: [[7], [7]]

```

I think I get why the second element of `copied` is altered, but I’m not sure why this doesn’t happen if `vecvec` is changed to `vec = [5]` and the second index removed in line 6. I also don’t know how to replace line 3 by a copy that is so deep that it ignores the duplication of references in line two and so yields `copied after change: [[7],[5]]`. Can anyone help?

And before anyone aks: I need to have a Vector of Vectors because in my application it is a population of chromosomes, each of which is itself a vector.

Thanks and best wishes,  
Niall.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [May 19, 2021, 2:12pm UTC](https://discourse.julialang.org/t/copying-cross-referenced-values/61442/2 "2021-05-19T14:12:48Z")

</div>

> [@Niall](#):
>
> I’m not sure why this doesn’t happen if `vecvec` is changed to `vec = [5]` and the second index removed in line 6

When you write `[vec;vec]` you are defining: “a vector that contains `vec` in its first element, and `vec` in its second element”. There is no hard binding between the first and second element of that vector; it just happened that you assigned the same object to both; but later on you can replace one element without changing the other. That’s what happens if you do:

```julia
vec = [5]
doubled = [vec;vec]
copied = deepcopy(doubled)
copied[1] = 7

```

(In the last line there you are telling: “replace the first element of `copied` by `7`”.)

On the other hand, with `copied[1][1] = 7` in your actual example, you are not replacing the object referred to by `copied[1]`, but _mutating_ it (actually, replacing its first index), so other references to the same object (as happens with `copied[2]` in this case) will reflect the same mutation.

> [@Niall](#):
>
> I also don’t know how to replace line 3 by a copy that is so deep that it ignores the duplication of references in line two and so yields `copied after change: [[7],[5]]`

Broadcasting:

```julia
copied = deepcopy.(doubled)

```

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [May 19, 2021, 2:45pm UTC](https://discourse.julialang.org/t/copying-cross-referenced-values/61442/3 "2021-05-19T14:45:48Z")

</div>

Oh, brilliant - thanks very much! 😃 I thought deepcopy was already doing the broadcasting.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [May 19, 2021, 2:57pm UTC](https://discourse.julialang.org/t/copying-cross-referenced-values/61442/4 "2021-05-19T14:57:48Z")

</div>

> [@Niall](#):
>
> I thought deepcopy was already doing the broadcasting.

What `deepcopy` ensures that the contents of the output are wholly independent from those of the input, but the structure is the same, so if the input contains multiple references to a single object, the output will also have that.

By the way, perhaps you would like to have independent copies of `[5]`, not only in `copied`, but also in `doubled`. If so, an alternative would be:

```julia
vec = [5]
doubled = [copy(vec) for _ = 1:2]
copied = deepcopy(doubled)
# ...

```

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [May 19, 2021, 3:20pm UTC](https://discourse.julialang.org/t/copying-cross-referenced-values/61442/5 "2021-05-19T15:20:58Z")

</div>

Hm. Thanks very much for the tip, but I think in my case it would raise my overhead. Before recombining parent chromosomes, I build up a careful system of references to parents that are selected for mating. These references can appear multiply, and are shuffled around in various ways, so I think it’s more efficient in my case to do the shuffling around using references, and only cash them out as independent objects right at the very end (using your broadcast deepcopy).
