# Deep copy of Array in Struct in NamedTuple

**URL:** https://discourse.julialang.org/t/deep-copy-of-array-in-struct-in-namedtuple/102349
**Category:** General Usage
**Tags:** array, struct, namedtuple, deepcopy
**Created:** [August 1, 2023, 11:38am UTC](https://discourse.julialang.org/t/deep-copy-of-array-in-struct-in-namedtuple/102349 "2023-08-01T11:38:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)
#### Post date: [August 1, 2023, 11:38am UTC](https://discourse.julialang.org/t/deep-copy-of-array-in-struct-in-namedtuple/102349/1 "2023-08-01T11:38:26Z")

</div>

I am creating and returning a NamedTuple `cache`

```julia
mystruct = MyStruct{Float64}(42, 42, 42)
cache = (; foo, bar, mystruct)
return cache

```

where `mystruct` is an object of the `mutable struct MyStruct` which contains an array

```julia
mutable struct MyStruct{uEltype <: Real}
  arr::Array{uEltype}

  function MyStruct{uEltype}(n1::Integer, n2::Integer, n3::Integer) where {uEltype <: Real}
      new(Array{uEltype}(undef, n1, n2, n3))
  end
end

```

When I later access `cache.mystruct.arr` in a different function it seems that `arr` is re-allocated, which I would avoid - ideally, I allocate storage once in `mystruct = MyStruct{Float64}(42, 42, 42)` and can access that later on.

In contrast, however, if I make `arr` a member of the named tuple itself, i.e.,

```julia
arr = Array{Float64}(undef, 42, 42, 42)
cache = (; foo, bar, arr)
return cache

```

no re-allocations are necessary when `cache.arr` is accessed later on.  
What is going on here? Is the memory reserved by `mystruct = MyStruct{Float64}(42, 42, 42)` considered out of scope once this function is left?

---

<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: [August 1, 2023, 11:54am UTC](https://discourse.julialang.org/t/deep-copy-of-array-in-struct-in-namedtuple/102349/2 "2023-08-01T11:54:09Z")

</div>

Can you show how you determine that `arr` is being re-allocated upon access?

Apart from that, consider adjusting the type of the `arr` member of `MyStruct` to more specific `arr::Array{uEltype, 3}` (3-dimensional array). Also, if you intend to keep a single array, you don’t have to declare `MyStruct` as `mutable`: `mutable` allows you to set `arr` to a whole new array, but if you just want to change the elements of `arr`, you don’t need `mutable`.

---

<div class="post-metadata">

### Author: ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)
#### Post date: [August 1, 2023, 12:08pm UTC](https://discourse.julialang.org/t/deep-copy-of-array-in-struct-in-namedtuple/102349/3 "2023-08-01T12:08:53Z")

</div>

> consider adjusting the type of the `arr` member of `MyStruct` to more specific `arr::Array{uEltype, 3}` (3-dimensional array)

That actually did the trick (for whatever reason), thanks a lot!

---

<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: [August 1, 2023, 12:17pm UTC](https://discourse.julialang.org/t/deep-copy-of-array-in-struct-in-namedtuple/102349/4 "2023-08-01T12:17:47Z")

</div>

I am glad it helped.

You probably observed unexpected allocations somewhere in your program. The original code was not type-stable, which is likely the reason. However, it does not mean that the whole array was re-allocated.
