# StructArrays Memory Layout

**URL:** <https://discourse.julialang.org/t/structarrays-memory-layout/65105>\
**Category:** New to Julia\
**Tags:** question, structarrays\
**Created:** [July 22, 2021, 1:23pm UTC](https://discourse.julialang.org/t/structarrays-memory-layout/65105 "2021-07-22T13:23:47Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [July 26, 2021, 8:40am UTC](https://discourse.julialang.org/t/structarrays-memory-layout/65105/14 "2021-07-26T08:40:34Z")

</div>

So, if I understand correctly, the problem is that you need to use the same data to both be a `StructArray{T}` and a `Matrix{Float64}`. You are correct that the method

```julia
julia> struct S
         x::Float64
         y::Float64
       end

julia> StructArray(S(rand(),rand()) for i in 1:10000);

```

will guarantee that each column is stored contiguously, but not that they are next to each other (they actually can’t be in general, because you can `push!` to each column).

> [@mdtisdall](#):
>
> Interesting! So does this imply I need to roll my own StructArray-ish struct if I want to have the fields packed “end-to-end” as well?

What you could do is the following

```julia
julia> struct S
         x::Float64
         y::Float64
       end

julia> data = rand(100, 2); # data with SoA layout

julia> s = StructArray{S}(data, dims=2) # this is just a view and does not allocate memory
100-element StructArray(view(::Matrix{Float64}, :, 1), view(::Matrix{Float64}, :, 2)) with eltype S:
 S(0.6656012326162104, 0.3785828010062877)
 S(0.7640633036875235, 0.7384631357534945)
 ⋮
 S(0.5863124494004175, 0.927251229499223)
 S(0.22170197402130576, 0.8404744601671492)

```

This is similar to your first attempt, but you need to pass the matrix with the correct memory layout (each row is a struct) to be turned effectively into a `StructArray`. Otherwise, in your first example, each vector of the `StructArray` was a non-contiguous view (a `row` of the original matrix).

---

_[View the full topic](https://discourse.julialang.org/t/structarrays-memory-layout/65105)._
