# Resize arrays in mutable structs

**URL:** <https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701>\
**Category:** General Usage\
**Created:** [November 6, 2020, 7:24pm UTC](https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701 "2020-11-06T19:24:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)\
**Post date:** [November 6, 2020, 7:24pm UTC](https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701/1 "2020-11-06T19:24:48Z")

</div>

In my Deldir package I use `ccall` on a code with 17 arguments that are all vectors. I have collected those in a `mutable struct`. When executing the `ccall` the content of the vectors in the struct are replaced.

If the Fortran code returns errors I have to increase the size of some of the arrays and use `ccall` again. This is the reason I use a _mutable_ struct. But do I risk running into problems with a `resize!` used directly on a field, i.e., like this:

```julia
mutable struct Foo
    a::Vector{Float64}
end

foo = Foo(rand(2))
resize!(foo.a, 3)

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 6, 2020, 7:28pm UTC](https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701/2 "2020-11-06T19:28:49Z")

</div>

I am not sure if I understand your question. However, it seems to me that you do not need to use a `mutable struct` only because you may call `resize!` over some `Vector`s that are in the `struct` fields. If the `struct` was immutable you could call `resize!` over the `Vector` fields anyway.

---

<div class="post-metadata">

**Author:** ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)\
**Post date:** [November 7, 2020, 12:12pm UTC](https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701/3 "2020-11-07T12:12:59Z")

</div>

> [@robertdj](#):
>
> ```julia
> resize!(foo.a, 3)
> 
> ```

I didn’t realize that was possible with a `struct` - thanks 🙂  
I’m not sure if something breaks subtly by resizing vectors in a (mutable) struct.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 7, 2020, 12:32pm UTC](https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701/4 "2020-11-07T12:32:31Z")

</div>

I have to admit that I have never used `ccall`, however, there should exist no difference between vectors created originally with some size, and `resize`d vectors. So it is hard to me to see a case where this matters. Are the vectors created in the Julia side? Fortran save some state between the calls (like the pointer)? Seem to me like a safe thing to do. But I think the house specialist is @yuyichao.

---

<div class="post-metadata">

**Author:** ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)\
**Post date:** [November 7, 2020, 6:35pm UTC](https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701/5 "2020-11-07T18:35:44Z")

</div>

The vectors are created in Julia and modified by Fortran. That part seems to work. Depending on the output I may have to increase the size of the array – also in Julia – and call the Fortran code again.

It has not caused any problem yet. I was just wondering if this way of modifying an instance of a struct could cause issues.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 7, 2020, 10:58pm UTC](https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701/6 "2020-11-07T22:58:23Z")

</div>

There is absolutely no problem with resizing a `Vector` contained in a `struct` or `mutable struct` in Julia. It’s a perfectly normal and common thing to do. The only potential issue is if some other part of your program assumed that the exact memory address of that vector’s data would be constant forever, since resizing a `Vector` can cause its underlying data to be moved around. This isn’t a problem for purely Julia code, but it could be an issue when interacting with C/FORTRAN.

For example, if you passed that `Vector` as a `Ptr{Float64}` to your FORTRAN code, _and_ that code stored that address for later use, then you could end up in a situation where your FORTRAN code was still pointing to the old data address after the resize.

From what I can tell, it doesn’t sound like you’re doing that, so I don’t see any problem.

---

<div class="post-metadata">

**Author:** ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)\
**Post date:** [November 8, 2020, 7:27pm UTC](https://discourse.julialang.org/t/resize-arrays-in-mutable-structs/49701/7 "2020-11-08T19:27:36Z")

</div>

Okay. Thanks a lot for clarifying 🙂
