In this case, I believe sizeof is returning the size of a pointer to the array, which on a 64 bit system is 8 bytes.
Since the array is heap allocated and tracked by the GC, it cannot be stored inline in the struct. The struct therefore contains a reference that points to the healp allocated object. It is for this same reason that isbits will return false on the second struct.
In this case, I believe sizeof is returning the size of a pointer to the array, which on a 64 bit system is 8 bytes.
This makes sense.
Is this the expected/correct behavior for sizeof() though?
Iβm using Julia 1.9.0, and this is the documentaiton:
sizeof(T::DataType)
sizeof(obj)
Size, in bytes, of the canonical binary representation of the given DataType T, if any. Or the size, in bytes, of object obj if it is not a DataType.
See also Base.summarysize.
Examples
β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘
julia> sizeof(Float32)
4
julia> sizeof(ComplexF64)
16
julia> sizeof(1.0)
8
julia> sizeof(collect(1.0:10.0))
80
julia> struct StructWithPadding
x::Int64
flag::Bool
end
julia> sizeof(StructWithPadding) # not the sum of `sizeof` of fields due to padding
16
julia> sizeof(Int64) + sizeof(Bool) # different from above
9
If DataType T does not have a specific size, an error is thrown.
julia> sizeof(AbstractArray)
ERROR: Abstract type AbstractArray does not have a definite size.
Stacktrace:
[...]
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
sizeof(str::AbstractString)
Size, in bytes, of the string str. Equal to the number of code units in str multiplied by the size, in bytes, of one code unit in str.
Examples
β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘
julia> sizeof("")
0
julia> sizeof("β")
3