# Arrays of concrete, mutable structs are not stored contiguously in memory

**URL:** https://discourse.julialang.org/t/arrays-of-concrete-mutable-structs-are-not-stored-contiguously-in-memory/99423
**Category:** General Usage
**Tags:** memory-allocation, struct
**Created:** [May 26, 2023, 5:37am UTC](https://discourse.julialang.org/t/arrays-of-concrete-mutable-structs-are-not-stored-contiguously-in-memory/99423 "2023-05-26T05:37:29Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![aleferna12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aleferna12/32/49549_2.png) [@aleferna12](https://discourse.julialang.org/u/aleferna12)
#### Post date: [May 26, 2023, 5:37am UTC](https://discourse.julialang.org/t/arrays-of-concrete-mutable-structs-are-not-stored-contiguously-in-memory/99423/1 "2023-05-26T05:37:29Z")

</div>

Hey!

Can someone explain to me why arrays of mutable structs are stored as arrays of pointers in memory? Example:

```julia
struct A x::Int32 end
mutable struct B x::Int32 end

a = Array{A}(undef, 2)
b = Array{B}(undef, 2)

println(Signed(pointer(a, 2) - pointer(a))) # Prints 4, because is an array of Int32s
println(Signed(pointer(b, 2) - pointer(b))) # Prints 8, because is an array of pointers

```

If it knows my struct B must hold a single field of size 4, what stops julia from making contiguous arrays of B structs?

Cheers

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [May 26, 2023, 2:37pm UTC](https://discourse.julialang.org/t/arrays-of-concrete-mutable-structs-are-not-stored-contiguously-in-memory/99423/2 "2023-05-26T14:37:02Z")

</div>

I must be able to read a mutable struct from an array, change its value, and have that change reflected in both the in-hand version and the original version in the array. This is why mutable values are stored as pointers – because you must be able to pass them anywhere and have their effects seen everywhere.

I suppose the reason that the array location in memory can’t serve as the “one actual instance” of the value is that an array can be moved, shrunk, or removed arbitrarily (via `push!`, `pop!`, `resize!`, `empty!`, etc, or being discovered to be garbage for the GC) but that doesn’t mean all the values it contains can be moved or destroyed. Moving/removing the value would invalidate any pointers to it and enable invalid memory access that would be very difficult to track or mitigate.

Immutable structs, on the other hand, do not have this behavior so are stored inline in arrays.
