# Push! seems to be incorrect in this situation

**URL:** <https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589>\
**Category:** General Usage\
**Tags:** question, deepcopy\
**Created:** [November 27, 2019, 4:51pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589 "2019-11-27T16:51:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ke\_xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ke_xu/32/8871_2.png) [@ke\_xu](https://discourse.julialang.org/u/ke_xu)\
**Post date:** [November 27, 2019, 4:51pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589/1 "2019-11-27T16:51:03Z")

</div>

```julia
function Append(vectorOfVector, vector)
    vector[1] = 0
    push!(vectorOfVector,vector)
    vector[1] = 1
    push!(vectorOfVector,vector)
end

vectorOfVectors = Vector{Vector{Int64}}()
vector = Vector{Int64}(undef,1)
Append(vectorOfVectors, vector)
vectorOfVectors

```

My Julia version is `1.2.0`. When I try to push vectors to a vector container by the function above, the output are not as expected:

```julia
2-element Array{Array{Int64,1},1}:
 [1]
 [1]

```

Should not the variable `vectorOfVectors` turns out to be `[[0],[1]]` ?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 27, 2019, 4:59pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589/2 "2019-11-27T16:59:06Z")

</div>

Welcome @ke_xu !

You may have a look into this proposal for your function:

```julia
function Append!(vectorOfVector, vector)
    vector[1] = 0
    push!(vectorOfVector,deepcopy(vector))
    vector[1] = 1
    push!(vectorOfVector,deepcopy(vector))
end

```

And you can find an explanation here (it’s better than what I could explain with my own words):  
[https://docs.julialang.org/en/v1.2/manual/arrays/](https://docs.julialang.org/en/v1.2/manual/arrays/)

at the beginning:

> In Julia, all arguments to functions are [passed by sharing](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) (i.e. by pointers). Some technical computing languages pass arrays by value, and while this prevents accidental modification by callees of a value in the caller, it makes avoiding unwanted copying of arrays difficult. By convention, a function name ending with a `!` indicates that it will mutate or destroy the value of one or more of its arguments (compare, for example, [`sort`](https://docs.julialang.org/en/v1.2/base/sort/#Base.sort) and [`sort!`](https://docs.julialang.org/en/v1.2/base/sort/#Base.sort!)). Callees must make explicit copies to ensure that they don’t modify inputs that they don’t intend to change. Many non- mutating functions are implemented by calling a function of the same name with an added `!` at the end on an explicit copy of the input, and returning that copy.

```julia
julia> function Append!(vectorOfVector, vector)
           vector[1] = 0
           push!(vectorOfVector,deepcopy(vector))
               vector[1] = 1
           push!(vectorOfVector,deepcopy(vector))
       end
Append! (generic function with 1 method)

julia> vectorOfVectors = Vector{Vector{Int64}}()
0-element Array{Array{Int64,1},1}

julia> vector = Vector{Int64}(undef,1)
1-element Array{Int64,1}:
 0

julia> Append!(vectorOfVectors, vector)
2-element Array{Array{Int64,1},1}:
 [0]
 [1]

```

---

<div class="post-metadata">

**Author:** ![ke\_xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ke_xu/32/8871_2.png) [@ke\_xu](https://discourse.julialang.org/u/ke_xu)\
**Post date:** [November 27, 2019, 5:18pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589/3 "2019-11-27T17:18:06Z")

</div>

Hi oheil,  
Thanks for your replying. I tested your code and it works well.  
But I don’t really understand why making a copy of `vector` can make change. If use `vector` directly (`not copy(vector)`), it should be `[0]` and `[1]` before being pushed in the two times respectively. So the two `push!` functions equal `push!(vectorOfVector, [0])` and `push!(vectorOfVector, [1])`. But how the second-time `push!` function changed the first value of `vectorOfVector`?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 27, 2019, 5:28pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589/4 "2019-11-27T17:28:29Z")

</div>

The line

```julia
push!(vectorOfVector,vector)

```

pushes just a pointer to `vector` into the container.  
Than the line

```julia
vector[1] = 1

```

changes the value at index 1 of your one and only existing `vector`.

In other words:

In those four lines:

```julia
vector = Vector{Int64}(undef,1)
vector[1] = 0
push!(vectorOfVector,vector)
push!(vectorOfVector,vector)

```

we only have a single instance/memory allocation of `vector`. Every change on a value at some index of `vector` will be visible in `vector` itself, at index 1 of `vectorOfVector` and at index 2 of `vectorOfVector`, because its all pointing to the same memory.

> So the two push! functions equal push!(vectorOfVector, [0]) and push!(vectorOfVector, [1]).

And thats not true. [0] and [1] above are two different vectors/arrays pointing to different locations in memory.

---

<div class="post-metadata">

**Author:** ![ke\_xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ke_xu/32/8871_2.png) [@ke\_xu](https://discourse.julialang.org/u/ke_xu)\
**Post date:** [November 27, 2019, 5:42pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589/5 "2019-11-27T17:42:36Z")

</div>

Thank you very much.  
Variable `vectorOfVector` just contained the pointers to `value`.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 27, 2019, 7:43pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589/6 "2019-11-27T19:43:33Z")

</div>

This is only true because in this case “value” is a vector/array.  
See e.g.:

```julia
julia> vectorOfAnything = Vector{Any}()
0-element Array{Any,1}

julia> vector = Vector{Int64}(undef,1)
1-element Array{Int64,1}:
 292839264

julia> vector[1]=0
0

julia> push!(vectorOfAnything,vector)
1-element Array{Any,1}:
 [0]

julia> x=0
0

julia> push!(vectorOfAnything,x)
2-element Array{Any,1}:
  [0]
 0

julia> push!(vectorOfAnything,vector)
3-element Array{Any,1}:
  [0]
 0
  [0]

julia> x=1
1

julia> push!(vectorOfAnything,x)
4-element Array{Any,1}:
  [0]
 0
  [0]
 1

julia> vector[1]=1
1

julia> x=0
0

julia> vectorOfAnything
4-element Array{Any,1}:
  [1]
 0
  [1]
 1

julia> 

```

---

<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:** [November 27, 2019, 7:52pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589/7 "2019-11-27T19:52:08Z")

</div>

You can think of it as always being true. Doing `x=` something wouldn’t change the value in the array even in the case where the value is an array. In fact, an assignment can never have an observable effect in some other object.

It will never be wrong to think of all objects being passed via pointer (modulo C interop). Some objects just happen to be immutable.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 27, 2019, 7:56pm UTC](https://discourse.julialang.org/t/push-seems-to-be-incorrect-in-this-situation/31589/8 "2019-11-27T19:56:43Z")

</div>

🤔 This needs some time to digest 😆
