# Static arrays being mutated, what is going on?

**URL:** <https://discourse.julialang.org/t/static-arrays-being-mutated-what-is-going-on/49519>\
**Category:** General Usage\
**Created:** [November 3, 2020, 12:40pm UTC](https://discourse.julialang.org/t/static-arrays-being-mutated-what-is-going-on/49519 "2020-11-03T12:40:13Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [November 3, 2020, 12:40pm UTC](https://discourse.julialang.org/t/static-arrays-being-mutated-what-is-going-on/49519/1 "2020-11-03T12:40:13Z")

</div>

I do not understand how `v3` is the code below is being allowed to mutate. What is going on there?

```julia
using StaticArrays
using BenchmarkTools

function test(v1, v2)
    v3 = zeros(SVector{3,Float64})
    for i in 1:10^4
      v3 = (v1 .* v2) .+ v3
    end
    v3
end

v1 = rand(SVector{3,Float64});
v2 = rand(SVector{3,Float64});

test(v1,v2)

@btime test($v1,$v2)

```

And it does not allocate anything (which also surprises me, as I thought at least that `v3` should have been allocated ,it is allocated in the `stack` and, thus, do not count? )

```julia
julia> @btime test($v1,$v2)
  10.025 μs (0 allocations: 0 bytes)
3-element SArray{Tuple{3},Float64,1,3} with indices SOneTo(3):
 5655.280099799662
  761.1553139585608
 1664.2491656557113

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 3, 2020, 12:56pm UTC](https://discourse.julialang.org/t/static-arrays-being-mutated-what-is-going-on/49519/2 "2020-11-03T12:56:19Z")

</div>

They are not mutated, each subsequent `v3` is a new value, not unlike an integer or a float (hence the lack of allocation).

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [November 3, 2020, 12:56pm UTC](https://discourse.julialang.org/t/static-arrays-being-mutated-what-is-going-on/49519/3 "2020-11-03T12:56:33Z")

</div>

Yes, `StaticArrays` are allocated on the stack (because their size is known) and therefore don’t “count”.

`v3` isn’t actually mutated. `v3 = (v1 .* v2) .+ v3` creates a new vector and binds `v3` to it. `v3` old bind is thrown away.

Edit: to make it more clear: the array that is bound to the name `v3` isn’t mutated, it’s exchanged for a whole new array, which is bound to the name `v3` instead.

---

<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:** [November 3, 2020, 1:03pm UTC](https://discourse.julialang.org/t/static-arrays-being-mutated-what-is-going-on/49519/4 "2020-11-03T13:03:23Z")

</div>

The _any_ `integer` analogy is very good, thanks.

The fact that this is not possible (and if with a mutable vector, slower) is something that I have to get familiar with:

```julia
function test(v1, v2)
    v3 = zeros(SVector{3,Float64})
    for i in 1:10^4
      for i in 1:3
        v3[i] = (v1[i] * v2[i]) + v3[i]
      end
    end
    v3
end

```

Uhm… this works (and is fast):

```julia
julia> v = [rand(SVector{3,Float64}) for i in 1:10_000];

julia> function sumv!(v)
         for i in 2:length(v)
           v[i] = v[i] + v[i-1]
         end
       end
sumv! (generic function with 1 method)

julia> sumv!(v)

```

Now I see many other ways of using `StaticArrays`… I was thinking that they were much more limited.
