# How to "unshare" an array?

**URL:** <https://discourse.julialang.org/t/how-to-unshare-an-array/66441>\
**Category:** General Usage\
**Created:** [August 15, 2021, 3:44pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441 "2021-08-15T15:44:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [August 15, 2021, 3:44pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441/1 "2021-08-15T15:44:20Z")

</div>

I want to efficiently build up a large matrix in a loop. At every iteration I will:

1. Perform some linear algebra operations on the matrix (it’s more complicated than the example below), and
2. Append a new row to the matrix.

The problem I have is that whenever I reshape my array buffer into a matrix, it gets marked as “shared”, even though the matrix representation is temporary and goes out of scope.

Is there a way to “unshare” the array? Or is there a better approach to this problem?

```julia
julia> x = rand(2);

julia> for _ in 1:10000
        println(reshape(x, 2, :) |> y -> y * y')
        append!(x, rand(2))
       end
[0.009747815392725486 0.039514699187714185; 0.039514699187714185 0.16018065474042284]
ERROR: cannot resize array with shared data
Stacktrace:
 [1] _growend!
   @ ./array.jl:922 [inlined]
 [2] append!(a::Vector{Float64}, items::Vector{Float64})
   @ Base ./array.jl:1019
 [3] top-level scope
   @ REPL[658]:3

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 15, 2021, 4:23pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441/2 "2021-08-15T16:23:26Z")

</div>

looks like a bug, please file an issue on github

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 15, 2021, 5:59pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441/3 "2021-08-15T17:59:31Z")

</div>

I think the relevant issue is [here](https://github.com/JuliaLang/julia/issues/33143).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 15, 2021, 6:51pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441/4 "2021-08-15T18:51:50Z")

</div>

this seems to be a weaker version, i.e. the previous `reshape` is never gonna be used again, we should be able to prove that.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [August 15, 2021, 7:58pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441/5 "2021-08-15T19:58:11Z")

</div>

Efficiently build up a matrix? Allocate the whole matrix in one operation…

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [August 15, 2021, 8:30pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441/6 "2021-08-15T20:30:58Z")

</div>

Good point. But I was also thinking of use cases where I want to limit the matrix to say 10,000 rows. So after reaching 10,000 rows, for each iteration before every push, I’d popfirst to maintain the size.

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [August 15, 2021, 8:46pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441/7 "2021-08-15T20:46:03Z")

</div>

I was recommended [ElasticArrays.jl](https://github.com/JuliaArrays/ElasticArrays.jl) when I had a similar question and it worked wonders for me.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [August 15, 2021, 8:49pm UTC](https://discourse.julialang.org/t/how-to-unshare-an-array/66441/8 "2021-08-15T20:49:35Z")

</div>

Or use a circular buffer (matrix). Aka queue. Julia has one ([https://github.com/Vexatos/CircularArrays.jl](https://github.com/Vexatos/CircularArrays.jl)).
