# Array of Mutable structs containing static arrays

**URL:** <https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187>\
**Category:** New to Julia\
**Tags:** struct\
**Created:** [February 13, 2021, 1:35am UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187 "2021-02-13T01:35:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)\
**Post date:** [February 13, 2021, 1:35am UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/1 "2021-02-13T01:35:59Z")

</div>

Hello,

I’m a new user coming from Python and C.

I would like to:

1. create a struct with arrays of known length as attribute.
2. Be able to update these arrays after the creation
3. Be able to allocate a multitude of these structs contiguously in memory
4. Copy this block to a CUDA device

I tried multiple packages like `StaticArrays` and `StructArrays` but I can’t seem to find a solution that satisfies all these constraints.

A `SVector`+`StaticArray` generates the struct I want but I’m not able to update the arrays.

To make it clearer for people with experience in python/numpy/c. This is what I want to achieve:

```julia
np.empty(5, dtype=np.dtype([
('field', (np.int32, 5)
])

```

or in C

```julia
struct test {
 int field[5];
};
calloc(5, sizeof(struct test));

```

Thank you for your help

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 13, 2021, 1:58am UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/2 "2021-02-13T01:58:42Z")

</div>

> [@Guillaume\_Leclerc](#):
>
> ```julia
> struct test {
> int field[5];
> };
> calloc(5, sizeof(struct test));
> 
> ```

The rough equivalent in Julia is:

```julia
struct Test
    field::SVector{5,Cint}
end
a = fill(Test(@SVector Cint[0,0,0,0,0]), 5)

```

This is stored contiguously in memory. You can change it with e.g.

```julia
a[3] = Test(@SVector Cint[1,3,5,7,9])

```

A more convenient way to mutate individual fields (without replacing the whole `struct`) is provided by e.g. [Setfield.jl](https://github.com/jw3126/Setfield.jl).

---

<div class="post-metadata">

**Author:** ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)\
**Post date:** [February 13, 2021, 3:07am UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/3 "2021-02-13T03:07:02Z")

</div>

Thanks for the answer!

Just out of curiosity. Why is there a need for an external library to perform such a basic operation?

---

<div class="post-metadata">

**Author:** ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)\
**Post date:** [February 13, 2021, 3:18am UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/4 "2021-02-13T03:18:43Z")

</div>

Oh my assumptions was that Setfield.jl was actually doing some unsafe casting to overcome the immutability of the struct. It’s just a syntactic sugar over doing a reassignment. I guess I just have to think of julia as a functional programming language and trust the compiler to do a good job generating efficient code 🙂

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [February 13, 2021, 10:48am UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/5 "2021-02-13T10:48:04Z")

</div>

The Julia equivalent of a Numpy array is a (typed) standard Julia array. Both have variable length.  
In this case, using a SVector from StaticArrays.jl improves performance because it allows additional optimizations (like avoiding heap allocations).

Edit: I just saw that you are using a custom dtype consisting of 5 integers, therefore the optimizations above are probably also done in Numpy.

---

<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:** [February 13, 2021, 11:32am UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/6 "2021-02-13T11:32:32Z")

</div>

Please correct me if I’m wrong, but isn’t a simple vector of immutable structs also a solution?

```julia
struct Test{T}
   i :: T
   j :: T
   k :: T
end
vec = Vector{Test{Int}}(undef,5)

```

```julia
julia> vec[1] = Test(1,1,1)
Test{Int64}(1, 1, 1)

```

I mean, isn’t this essentially equivalent to a vector of SVectors, just without the convenience of those?

---

<div class="post-metadata">

**Author:** ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)\
**Post date:** [February 13, 2021, 4:40pm UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/7 "2021-02-13T16:40:26Z")

</div>

I don’t think it is. I need an array of variable amount of structs that have constant (actually parametric but I was simplifying here) number of fields.

---

<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:** [February 13, 2021, 5:00pm UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/8 "2021-02-13T17:00:31Z")

</div>

(I would like the forum to have an option to fork the threads to ask stupid questions without polluting the OP, so sorry in advance).

Actually my question was related to the fact that the solution provided,

```julia
struct Test
  field :: SVector{5,Int}
end
a = fill(Test(@SVector Int[0,0,0,0,0]), 5)

```

seems somewhat redundant, since one can write directly

```julia
a = fill((SVector{5,Int}(0,0,0,0,0), 5)

```

and get a vector of constant-size vectors (at least in the minimal example I don’t see the need of the `Test` struct here).

Than you asked why one needs an external library, `StaticArrays` for that, and I was pointing that if the number of fields is known in advance, you don’t, you can use a vector of immutable structs. Of course, `StaticArrays` provides the convenience of the parametric number of fields and, furthermore, the implementation of many arithmetic operations, zeros, etc.

Also I think a vector of tuples can do the same:

```julia
julia> v = [(0,0) for i in 1:1_000_000];

julia> v[1]
(0, 0)

julia> function mutate_vec!(v)
         for i in 1:length(v)
           v[i] = (1,1)
         end
       end
mutate_vec! (generic function with 1 method)

julia> @allocated mutate_vec!(v) # compiles
842178

julia> @allocated mutate_vec!(v)
0

julia> v[1]
(1, 1)

```

As far as I understand (which is not much), that is contiguous in memory.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 13, 2021, 5:01pm UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/9 "2021-02-13T17:01:29Z")

</div>

> [@lmiq](#):
>
> Actually my question was related to the fact that the solution provided […] seems somewhat redundant […] (at least in the minimal example I don’t see the need of the `Test` struct here).

My understanding was that in the actual application the `struct` would have other fields as well.

---

<div class="post-metadata">

**Author:** ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)\
**Post date:** [February 13, 2021, 10:05pm UTC](https://discourse.julialang.org/t/array-of-mutable-structs-containing-static-arrays/55187/10 "2021-02-13T22:05:09Z")

</div>

@stevengj You are absolutely right, it’s just that I was trying to abstract my question as much as possible.

Thank you both for all your very helpful answers!
