# How to pre-allocate an Array of UnitRange (specifically a SharedArray of Tuple of UnitRange)?

**URL:** <https://discourse.julialang.org/t/how-to-pre-allocate-an-array-of-unitrange-specifically-a-sharedarray-of-tuple-of-unitrange/66506>\
**Category:** General Usage\
**Tags:** question, tuple, memory-allocation, distributed, sharedarrays\
**Created:** [August 16, 2021, 5:57pm UTC](https://discourse.julialang.org/t/how-to-pre-allocate-an-array-of-unitrange-specifically-a-sharedarray-of-tuple-of-unitrange/66506 "2021-08-16T17:57:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [August 16, 2021, 5:57pm UTC](https://discourse.julialang.org/t/how-to-pre-allocate-an-array-of-unitrange-specifically-a-sharedarray-of-tuple-of-unitrange/66506/1 "2021-08-16T17:57:55Z")

</div>

##### Question

Is there a shorter or better way to pre-allocate `indsTup` and `inds` as shown below? :

```julia-auto
indsTup = [(1:2,1:2)]
for i in 1:10
	push!(indsTup,(1:2,1:2))
end
indsTup = SharedArray(indsTup)

inds = [1:2]
for i in 1:10
	push!(inds,1:2)
end
inds = SharedArray(inds)

julia> typeof(inds) # how do I pre-allocate this?
SharedVector{UnitRange{Int64}} (alias for SharedArray{UnitRange{Int64}, 1})

julia> typeof(indsTup) # how do I pre-allocate this?
SharedVector{Tuple{UnitRange{Int64}, UnitRange{Int64}}} (alias for SharedArray{Tuple{UnitRange{Int64}, UnitRange{Int64}}, 1})

```

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 16, 2021, 7:33pm UTC](https://discourse.julialang.org/t/how-to-pre-allocate-an-array-of-unitrange-specifically-a-sharedarray-of-tuple-of-unitrange/66506/2 "2021-08-16T19:33:39Z")

</div>

The former is possible with `indsTup = SharedArray{typeof((1:2, 1:2))}((11,))`. The latter is similar and easier.

See [Shared Arrays · The Julia Language](https://docs.julialang.org/en/v1/stdlib/SharedArrays/)

Sample code:

```julia
using Distributed
using SharedArrays

rmprocs(procs()[2:end]) # Do not allow processes to proliferate unlimitedly on Jupyter, etc.
addprocs(4)

S = SharedArray{typeof((1:2, 1:2))}((10,))

```

Output:

```julia
10-element SharedVector{Tuple{UnitRange{Int64}, UnitRange{Int64}}}:
 (0:0, 0:0)
 (0:0, 0:0)
 (0:0, 0:0)
 (0:0, 0:0)
 (0:0, 0:0)
 (0:0, 0:0)
 (0:0, 0:0)
 (0:0, 0:0)
 (0:0, 0:0)
 (0:0, 0:0)

```

Code:

```julia
for k in eachindex(S)
    remotecall_fetch(workers()[mod1(k, nworkers())]) do
        pid = myid()
        S[k] = (pid:2pid, 3pid:4pid)
    end      
end
S

```

Output:

```julia
10-element SharedVector{Tuple{UnitRange{Int64}, UnitRange{Int64}}}:
 (2:4, 6:8)
 (3:6, 9:12)
 (4:8, 12:16)
 (5:10, 15:20)
 (2:4, 6:8)
 (3:6, 9:12)
 (4:8, 12:16)
 (5:10, 15:20)
 (2:4, 6:8)
 (3:6, 9:12)

```
