Why is sizeof(M.C) larger than the others? Tested with 0.5.0, 0.4.7 and 0.6.0-dev.2525.
julia> module M
immutable A
e::UInt32
a::UInt32
b::UInt64
end
immutable B
e::UInt64
a::UInt32
b::UInt32
end
immutable C
a::UInt32
e::UInt64
c::UInt32
end
immutable D
a::UInt32
c::UInt32
e::UInt64
end
immutable E
a::UInt64
e::UInt64
end
end
M
julia> sizeof(M.A)
16
julia> sizeof(M.B)
16
julia> sizeof(M.C)
24
julia> sizeof(M.D)
16
julia> sizeof(M.E)
16
Just to be clear, this is not unique to Julia. This is the same as what would happen if you declared a C struct with those types: the compiler adds padding inside the struct to make sure that data is properly aligned.
The fact that Julia properly aligns structure members like this is important for performance, and for calling C libraries.
If you want to read non-aligned binary data from a file, just use read. e.g. C(read(f,UInt32), read(f, UInt64), read(f, UInt32)). (Of course, for file i/o, you may also need to worry about endianness issues: you might want to call ntoh on the result of the read.)