# Preallocating memory for a 3 dimensional array

**URL:** <https://discourse.julialang.org/t/preallocating-memory-for-a-3-dimensional-array/112874>\
**Category:** New to Julia\
**Tags:** array, memory-allocation\
**Created:** [April 12, 2024, 12:07pm UTC](https://discourse.julialang.org/t/preallocating-memory-for-a-3-dimensional-array/112874 "2024-04-12T12:07:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jay/32/208446_2.png) [@Jay](https://discourse.julialang.org/u/Jay)\
**Post date:** [April 12, 2024, 12:07pm UTC](https://discourse.julialang.org/t/preallocating-memory-for-a-3-dimensional-array/112874/1 "2024-04-12T12:07:45Z")

</div>

Hi,  
I am new to Julia.  
I am writing a code where a 3 dimensional array, say A, each index with an initial size “chi”, is modified, and in the due course of time iteratively, (say 1000 times) the size of each index will grow gradually, from some “chi” to a bigger value “chi\_max”.

Is there a way to preallocate memory for a 3 d array of size “chi\_max” each at the start, so that there are no reallocations?  
I think this would cause considerable speed up of the code.  
Thanks,  
Jay.

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [April 12, 2024, 12:20pm UTC](https://discourse.julialang.org/t/preallocating-memory-for-a-3-dimensional-array/112874/2 "2024-04-12T12:20:14Z")

</div>

Yep! Depending on the type of data in the array there are some common functions. `zeros([Int | Float64 | ...], chi_max, chi_max, chi_max)` will give you a 3D array of zeros. Similarly, `falses` gives an array of `Bool`. They also have counterparts `ones` and `trues`.

For a more general array, and to save on initialising the values use

```julia
A = Array{TypeOfElement}(undef, chi_max, chi_max, chi_max)

```

where `TypeOfElement` is whatever type you want.

---

<div class="post-metadata">

**Author:** ![Jay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jay/32/208446_2.png) [@Jay](https://discourse.julialang.org/u/Jay)\
**Post date:** [April 12, 2024, 12:35pm UTC](https://discourse.julialang.org/t/preallocating-memory-for-a-3-dimensional-array/112874/3 "2024-04-12T12:35:34Z")

</div>

Hi,  
Thanks for your quick reply.  
Actually in my code I need to deal with a bunch of these kind of objects and there are a lot of tensor contractions involved which lead to increase in the size.

To begin with I would like to work with just (chi)^3 sized object rather than going over the whole (chi-max)^3 tensor as it leads to considerable improvement in the performance.

Is there no way to work with a (chi)^3 sized object but at the same time have a (chi\_max)^3 space allocated for it where rewrites keep happening?

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [April 12, 2024, 12:46pm UTC](https://discourse.julialang.org/t/preallocating-memory-for-a-3-dimensional-array/112874/4 "2024-04-12T12:46:56Z")

</div>

Views might be what you need?

```julia-repl
julia> A = ones(4, 4)
4×4 Matrix{Float64}:
 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0

julia> B = view(A, 1:2, 1:2)
2×2 view(::Matrix{Float64}, 1:2, 1:2) with eltype Float64:
 1.0 1.0
 1.0 1.0

julia> B * [1, 2] # B acts as 2x2 matrix
2-element Vector{Float64}:
 3.0
 3.0

julia> B[2, 2] = 9999 # But the underlying memory is shared with A
3

julia> A
4×4 Matrix{Float64}:
 1.0 1.0 1.0 1.0
 1.0 9999.0 1.0 1.0
 1.0 1.0 1.0 1.0
 1.0 1.0 1.0 1.0

```

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [April 12, 2024, 12:59pm UTC](https://discourse.julialang.org/t/preallocating-memory-for-a-3-dimensional-array/112874/5 "2024-04-12T12:59:04Z")

</div>

You may also want to look at [ResizableArrays](https://docs.juliahub.com/General/ResizableArrays/stable/), which may fit your use case.

---

<div class="post-metadata">

**Author:** ![Jay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jay/32/208446_2.png) [@Jay](https://discourse.julialang.org/u/Jay)\
**Post date:** [April 13, 2024, 11:47am UTC](https://discourse.julialang.org/t/preallocating-memory-for-a-3-dimensional-array/112874/6 "2024-04-13T11:47:36Z")

</div>

Hi,  
Thanks to both the quick replies.  
Both view and resizeablearrays seem to do the job.

I had another related question.

Say I had an array c1t := (n \* n \* n \* n). If I want to reshape it into something  
(n^2 \* n^2), then what would be the most efficient way to get this done in Julia?  
Should I use something like

```julia
c1t_r = reshape(c1t, size(c1t, 1)*size(c1t, 2), size(c1t, 3)*size(c1t, 4)) 

```

or

```julia
c1t = reshape(c1t, size(c1t, 1)*size(c1t, 2), size(c1t, 3)*size(c1t, 4))

```

or wherever I want to use the rank 2 tensor, substitute

```julia
reshape(c1t, size(c1t, 1)*size(c1t, 2), size(c1t, 3)*size(c1t, 4))

```

or something else?  
Thanks.
