# Deepcopy usage

**URL:** <https://discourse.julialang.org/t/deepcopy-usage/57009>\
**Category:** General Usage\
**Created:** [March 12, 2021, 9:23am UTC](https://discourse.julialang.org/t/deepcopy-usage/57009 "2021-03-12T09:23:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [March 12, 2021, 9:23am UTC](https://discourse.julialang.org/t/deepcopy-usage/57009/1 "2021-03-12T09:23:38Z")

</div>

In this code:

```julia
module MatrixOp

function func!(A, BB)
    BB[1][1,1] += 100
    A, BB...
end

func(A, B...) = func!(copy(A), deepcopy(B))

A = rand(1:10,3,3)
b = rand(1:10,3,1)
c = rand(1:10,3,1)

A2, b2, b3 = func(A,b,b)

@show b2
@show b3

end

```

why is `b2` and `b3` returned the same? I would assume `deepcopy()` would create individual copies for each of the elements of the `B` tuple. What am I missing?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 12, 2021, 9:59am UTC](https://discourse.julialang.org/t/deepcopy-usage/57009/2 "2021-03-12T09:59:51Z")

</div>

> [@owiecc](#):
>
> why is `b2` and `b3` returned the same?

Before the deepcopy, there is one array that is referred to two times in the `(b, b)` tuple. After the `deepcopy` there is _one_ new array and the elements in the new tuple refers to that new array. You can think of the result of `deepcopy` as if you would serialize the object, and then deserialize it back. It does not mean you get a copy of the object for every reference to it.

---

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [March 12, 2021, 10:39am UTC](https://discourse.julialang.org/t/deepcopy-usage/57009/3 "2021-03-12T10:39:11Z")

</div>

This is exactly my problem with `deepcopy`. Wouldn’t serialization break the reference between individual `b`s in the `(b, b)` tuple making it a `(b1, b2)`?

Is my solution then:

```julia
func(A, B...) = func!(copy(A), map(copy,B))

```

which is a poor man’s one-level-deep copy?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 12, 2021, 10:44am UTC](https://discourse.julialang.org/t/deepcopy-usage/57009/4 "2021-03-12T10:44:33Z")

</div>

> [@owiecc](#):
>
> Wouldn’t serialization break the reference between individual `b` s in the `(b, b)` tuple making it a `(b1, b2)` ?

No, because the resulting data structure from serialization / deserialization would then have very different properties from the original one. You want the result of `deepcopy` to give you something that behaves the same as what you had, but with no shared references to the original object.

The solution is probably to either:

- Not create the aliased data structure to being with
- Write something custom that does what you want for this case

---

<div class="post-metadata">

**Author:** ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)\
**Post date:** [March 12, 2021, 10:59am UTC](https://discourse.julialang.org/t/deepcopy-usage/57009/5 "2021-03-12T10:59:38Z")

</div>

That actually makes sense. I had a different mental model on what serialisation/deserialisation should do. It makes sense that all relationships and metadata on what do the bits mean are preserved while the actual bits are copied to a new location.

Thanks for your help.
