Consider this:
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…
open(...) do fp
dummy = Ref{Dummy34}()
read!(fp, dummy)
position(fp) # --> 40
end
I guess I needed to do it explicitly
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 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