Why doesn't modifying a shallow copy of an array reflect on the original?

You may understand shallow as “just one level” and deep as “all levels”.

First let us have a vector of vectors, a shallow copy of it, and a deep copy of it.

julia> a1, a2, a3 = [1, 2, 3], [4, 5, 6], [7, 8, 9]
([1, 2, 3], [4, 5, 6], [7, 8, 9])

julia> a123 = [a1, a2, a3]
3-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]

julia> shallow = copy(a123);

julia> deep = deepcopy(a123);

Now, if we mutate the first element inside the original vector (a123), this reflects in the shallow but not in the deep.

julia> a123[1][1] = 10
10

julia> shallow[1]
3-element Array{Int64,1}:
 10
  2
  3

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

The reason is that shallow has allocated a new vector with the same three inner vectors, i.e., it copied just the first level that is the external vector, but did not copy the second level that is each element. shallow has the same elements as a123 inside it. The deepcopy has created a copy of each element thus remain unaffected.

To check if the copy has really copied the first level (i.e., the outer vector), we can change the outer vector (instead of the elements inside it) and see the changes do not reflect in the original.

julia> shallow[1] = [30, 20, 10];

julia> shallow
3-element Array{Array{Int64,1},1}:
 [30, 20, 10]
 [4, 5, 6]
 [7, 8, 9]

julia> a123
3-element Array{Array{Int64,1},1}:
 [10, 2, 3]
 [4, 5, 6]
 [7, 8, 9]
1 Like