# Values of arrays changed unexpectedly due to storage allocation issue?

**URL:** <https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [December 15, 2021, 11:11am UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135 "2021-12-15T11:11:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaelGao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michaelgao/32/27370_2.png) [@MichaelGao](https://discourse.julialang.org/u/MichaelGao)\
**Post date:** [December 15, 2021, 11:11am UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135/1 "2021-12-15T11:11:39Z")

</div>

Not sure whether I am asking this question in the correct section.  
I wanted to build a solver for the 1D advection-dispersion-equation, with Dirichlet boundary condition at the influx end and Neumann BC at the efflux end. The code below was just for a test so there are some hardcoding.  
First, a domain with n nodes was initiated as `A`. Then the function `SolveNADE()` solves the ADE numerically once, by delta Δt. As I have expected, the initial array `A` should not be changed since I am traversing the array `C_over_Domain`. However, this is not the case. The `A` would have new values after each ` for i in 1:5000000` loop. What could be the reason and how should I avoid this? Thanks.

```julia
nodesN = 100
A = zeros(nodesN)

# the physical condition, alias: PhyCond in functions
struct PhysicalCondition
    u::Float64 # advection velocity [m/s]
    D::Float64 # diffusion coefficient [m²/s]
end
# numerical scheme, alias: Scheme in functions
struct NumericalScheme
    Δx::Float64
    Δt::Float64
end

𝒖 = 4e-6 # advection velocity [m/s]
𝑫 = 1e-9 # diffusion coefficient [m²/s]
phycondition = PhysicalCondition(𝒖, 𝑫)
dx = 1
dt = 1
numericscheme = NumericalScheme(dx, dt)

function SolveNADE(PhyCond, Scheme, domain)
    C_next = domain
    C_next[1] = 1
    for i in (2:size(C_next)[1]-1)
        C_next[i] = (- PhyCond.u * Scheme.Δt / Scheme.Δx * (domain[i] - domain[i-1]) 
                        + PhyCond.D * Scheme.Δt / Scheme.Δx^2 * (domain[i-1] - 2*domain[i] + domain[i+1])
                        + domain[i])
    end
    C_next[end] = (- PhyCond.u * Scheme.Δt / Scheme.Δx * (domain[end] - domain[end-1]) 
                        + PhyCond.D * Scheme.Δt / Scheme.Δx^2 * (domain[end-1] - domain[end])
                        + domain[end])
    return C_next
end

C_over_Domain = A
for i in 1:5000000
    new_domain = C_over_Domain
    C_over_Domain = SolveNADE(phycondition, numericscheme, new_domain)
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 15, 2021, 11:48am UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135/2 "2021-12-15T11:48:51Z")

</div>

> [@MichaelGao](#):
>
> `C_over_Domain = A`

You should probably `copy()` here:

```julia
C_over_Domain = copy(A)

```

---

<div class="post-metadata">

**Author:** ![MichaelGao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michaelgao/32/27370_2.png) [@MichaelGao](https://discourse.julialang.org/u/MichaelGao)\
**Post date:** [December 15, 2021, 12:01pm UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135/3 "2021-12-15T12:01:51Z")

</div>

Thanks! Now I´ve got the point.

---

<div class="post-metadata">

**Author:** ![MichaelGao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michaelgao/32/27370_2.png) [@MichaelGao](https://discourse.julialang.org/u/MichaelGao)\
**Post date:** [December 15, 2021, 12:04pm UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135/4 "2021-12-15T12:04:29Z")

</div>

Hope these further discussions & documentations could help other newcomers of Julia like me.

> [@Copy vector?](https://discourse.julialang.org/t/copy-vector/20082):
>
> I just realized this behavior: julia\> x = [0. , 0.] 2-element Array{Float64,1}: 0.0 0.0 julia\> y = x 2-element Array{Float64,1}: 0.0 0.0 julia\> y[1] = 1. 1.0 julia\> x 2-element Array{Float64,1}: 1.0 0.0 x and y are just different names for the same vector. How do I copy a vector into another one, then? Also, is there any style suggestion to avoid running into errors given that we could be modifying previous values of array variables this way?

[https://docs.julialang.org/en/v1/base/base/#=](https://docs.julialang.org/en/v1/base/base/#=)

---

<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:** [December 15, 2021, 12:07pm UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135/5 "2021-12-15T12:07:41Z")

</div>

Since a (not-so) old question of mine was tagged, this is related: [Assignment and mutation · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/assignment/)

---

<div class="post-metadata">

**Author:** ![MichaelGao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michaelgao/32/27370_2.png) [@MichaelGao](https://discourse.julialang.org/u/MichaelGao)\
**Post date:** [December 15, 2021, 3:18pm UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135/6 "2021-12-15T15:18:07Z")

</div>

A further question regarding “Assignment and Mutation”:  
Why is this happening? To obtain the results that I´m expecting, for each loop I have to make a copy.

```julia
x = [1, 3, 6]
emptysaver = []
for i in 1:3
    x[i] = x[i] + 10
    push!(emptysaver, x)
end
emptysaver

3-element Vector{Any}:
 [11, 13, 16]
 [11, 13, 16]
 [11, 13, 16]

```

```julia
x = [1, 3, 6]
emptysaver = []
for i in 1:3
    x[i] = x[i] + 10
    new = copy(x)
    push!(emptysaver, new)
end
emptysaver

3-element Vector{Any}:
 [11, 3, 6]
 [11, 13, 6]
 [11, 13, 16]

```

---

<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:** [December 15, 2021, 3:41pm UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135/7 "2021-12-15T15:41:04Z")

</div>

Vectors are passed by sharing, so when you push a vector to a collection of vectors, you are passing the reference to that vector. Thus, the reference to the same vector is being pushed every time.

To push the reference of a new vector, you need to first create the new vector, by copying.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 15, 2021, 4:57pm UTC](https://discourse.julialang.org/t/values-of-arrays-changed-unexpectedly-due-to-storage-allocation-issue/73135/9 "2021-12-15T16:57:34Z")

</div>

It is often helpful to write this as a comprehension, which will create new object (rather than copy references) and also yield the right type (note your vector is a `Vector{Any}` which kills performance, don’t initialise vectors with `[]`):

```julia
julia> [x .+ 10 for _ ∈ 1:3]
3-element Vector{Vector{Int64}}:
 [11, 13, 16]
 [11, 13, 16]
 [11, 13, 16]

```
