# Large matrix crashes StaticArrays

**URL:** <https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689>\
**Category:** General Usage\
**Tags:** bug, staticarrays\
**Created:** [April 6, 2021, 3:00pm UTC](https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689 "2021-04-06T15:00:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alequa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alequa/32/12338_2.png) [@alequa](https://discourse.julialang.org/u/alequa)\
**Post date:** [April 6, 2021, 3:00pm UTC](https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689/1 "2021-04-06T15:00:01Z")

</div>

Hi,  
I would like to instantiate a medium-size (1000,1000) matrix in a StaticArray field of MyStruct. But it crashes!  
Can you tell me if I am doing something wrong?

```julia
a = SMatrix{1000,1000, Float64}(rand(1000,1000))

syntax: expression too large
Stacktrace:
 [1] top-level scope at /home/cocconat/Documents/Research/phd_project/simulations/tripod_network/weights/dataStructs.jl:96
 [2] _convert at /home/cocconat/.julia/packages/StaticArrays/LJQEe/src/convert.jl:35 [inlined]
 [3] convert at /home/cocconat/.julia/packages/StaticArrays/LJQEe/src/convert.jl:32 [inlined]
 [4] SArray{Tuple{1000,1000},Float64,2,L} where L(::Array{Float64,2}) at /home/cocconat/.julia/packages/StaticArrays/LJQEe/src/convert.jl:7
 [5] top-level scope at /home/cocconat/Documents/Research/phd_project/simulations/tripod_network/weights/dataStructs.jl:96

```

Thanks

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 6, 2021, 3:08pm UTC](https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689/2 "2021-04-06T15:08:15Z")

</div>

Static arrays are not meant for big matrices/vectors but only up to about 100 elements. I think this is essentially Julia’s parser giving up to parse an expression for a 1-million element tuple (which is the underlying storage for StaticArrays).

---

<div class="post-metadata">

**Author:** ![alequa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alequa/32/12338_2.png) [@alequa](https://discourse.julialang.org/u/alequa)\
**Post date:** [April 6, 2021, 3:28pm UTC](https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689/3 "2021-04-06T15:28:36Z")

</div>

you are right, it is also written in the README

---

<div class="post-metadata">

**Author:** ![alan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alan/32/15970_2.png) [@alan](https://discourse.julialang.org/u/alan)\
**Post date:** [July 16, 2022, 5:36pm UTC](https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689/4 "2022-07-16T17:36:18Z")

</div>

Why does StaticArrays fail with large matrices? Does it allocate on the stack rather than the heap?

Should we be using Julia’s default arrays for large matrices, even when they won’t need to be resized and speed is important?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 16, 2022, 6:47pm UTC](https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689/5 "2022-07-16T18:47:34Z")

</div>

> [@alan](#):
>
> Why does StaticArrays fail with large matrices? Does it allocate on the stack rather than the heap?

Some of the `StaticArray`s do. The `SArray`s are immutable and are allocated on the stack.

> **[Home · StaticArrays.jl](https://juliaarrays.github.io/StaticArrays.jl/stable/)**
>
> Documentation for StaticArrays.jl.

> [@alan](#):
>
> Should we be using Julia’s default arrays for large matrices, even when they won’t need to be resized and speed is important?

There are also mutable but statically sized arrays, `MArray`s. There is also `SizedArray` which can be used to add size information to a normal `Array`.

[https://juliaarrays.github.io/StaticArrays.jl/stable/pages/api/#StaticArraysCore.MArray](https://juliaarrays.github.io/StaticArrays.jl/stable/pages/api/#StaticArraysCore.MArray)

# SArray Demonstration

```julia
julia> using StaticArrays

julia> subtypes(StaticArray)
6-element Vector{Any}:
 FieldArray{N} where N<:Tuple
 MArray
 SArray
 SHermitianCompact
 SizedArray
 StaticArrays.SUnitRange

julia> sa = SArray{Tuple{2,2}}(ntuple(identity,4))
2×2 SMatrix{2, 2, Int64, 4} with indices SOneTo(2)×SOneTo(2):
 1 3
 2 4

julia> typeof(sa.data)
NTuple{4, Int64}

julia> isimmutable(sa)
true

julia> isimmutable(sa.data)
true

julia> sa[1] = 2
ERROR: setindex!(::SMatrix{2, 2, Int64, 4}, value, ::Int) is not defined.
...

julia> pointer(sa)
ERROR: conversion to pointer not defined for SMatrix{2, 2, Int64, 4}

```

# MArray demonstration

```julia
julia> ma = MArray{Tuple{2,2}}(1:4)
2×2 MMatrix{2, 2, Int64, 4} with indices SOneTo(2)×SOneTo(2):
 1 3
 2 4

julia> isimmutable(ma)
false

julia> isimmutable(ma.data)
true

julia> ma[1] = 2
2

julia> ma
2×2 MMatrix{2, 2, Int64, 4} with indices SOneTo(2)×SOneTo(2):
 2 3
 2 4

julia> pma = pointer(ma)
Ptr{Int64} @0x00007f8ec8748a90

julia> unsafe_load(pma, 1)
2

julia> unsafe_load(pma, 2)
2

julia> unsafe_load(pma, 3)
3

julia> unsafe_load(pma, 4)
4

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 16, 2022, 7:11pm UTC](https://discourse.julialang.org/t/large-matrix-crashes-staticarrays/58689/6 "2022-07-16T19:11:55Z")

</div>

> [@alan](#):
>
> Should we be using Julia’s default arrays for large matrices

Yes. Or if they have special structure (symmetry, diagonal, etc), use some of the special wrappers in the LinearAlgebra stdlib.
