# Unexpected \`sizeof\` struct with long StaticArrays

**URL:** <https://discourse.julialang.org/t/unexpected-sizeof-struct-with-long-staticarrays/79797>\
**Category:** General Usage\
**Created:** [April 21, 2022, 6:51pm UTC](https://discourse.julialang.org/t/unexpected-sizeof-struct-with-long-staticarrays/79797 "2022-04-21T18:51:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [April 21, 2022, 6:51pm UTC](https://discourse.julialang.org/t/unexpected-sizeof-struct-with-long-staticarrays/79797/1 "2022-04-21T18:51:26Z")

</div>

Consider this:

```julia
using StaticArrays

struct Dummy
    B::SVector{34,UInt8}
end
@assert sizeof(Dummy) == sum(sizeof.(fieldtypes(Dummy)))

struct Dummy32
    A::Float32
    B::SVector{32,UInt8}
end
@assert sizeof(Dummy32) == sum(sizeof.(fieldtypes(Dummy32)))

struct Dummy34
    A::Float32
    B::SVector{34,UInt8}
end
@assert sizeof(Dummy34) == sum(sizeof.(fieldtypes(Dummy34)))

```

The last assertion fails, `sizeof(Dummy34)` return 40, 2 more bytes than expected. The behavior switched based on number of elements surprised me.

This was a problem, I needed to read some binary into a large struct quick, and it didn’t lined up…

```julia
open(...) do fp
    dummy = Ref{Dummy34}()
    read!(fp, dummy)
    position(fp) # --> 40
end

```

I guess I needed to do it explicitly

```julia
import Base.read
function read(fp::IOStream, ::Type{Dummy34})
    elem = [
        read(fp, f)
        for f in fieldtypes(Dummy34)
    ]
    return Dummy34(elm...)   
end

```

Any comments or suggestions would be nice, thank you.

```julia
Julia Version 1.6.3
Commit ae8452a9e0 (2021-09-23 17:34 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-8365U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

[90137ffa] StaticArrays v1.4.1

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 21, 2022, 7:21pm UTC](https://discourse.julialang.org/t/unexpected-sizeof-struct-with-long-staticarrays/79797/2 "2022-04-21T19:21:06Z")

</div>

The following seems to work for me

```julia
using StaticArrays

struct Dummy
    B::SVector{34,UInt8}
end
@assert sizeof(Dummy) == sum(sizeof.(fieldtypes(Dummy)))

struct Dummy32
    A::Float32
    B::SVector{32,UInt8}
end
@assert sizeof(Dummy32) == sum(sizeof.(fieldtypes(Dummy32)))

struct Dummy34
    A::Float32
    B::SVector{34,UInt8}
end

dummy1 = Dummy34(0.0, SVector((0 for _ in 1:34)...))
open("test.dat", "w") do fp
    write(fp, Ref(dummy1))
    @show position(fp) # --> 40
end

open("test.dat", "r") do fp
    dummy2 = Ref{Dummy34}()
    read!(fp, dummy2)
    @show position(fp) # --> 40
    @assert dummy2[] == dummy1
end

```

Regarding the size of `Dummy34`: I could imagine some kind of 32 bit [alignment](https://en.wikipedia.org/wiki/Data_structure_alignment).
