Are objects created by the special inner-constructor method new
guaranteed to be zero-initialized?
i.e.
julia> mutable struct Foo{N,T}
data::NTuple{N,T}
@inline function Foo{N,T}(::UndefInitializer) where {N,T}
@assert Base.allocatedinline(T)
new{N,T}()
end
end
julia> Foo{50,Int64}(undef)
Foo{50, Int64}((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
No, they are uninitialized
It's actually unspecified/not documented. Read the code, but it may change.
I would rely on them being uninitialized nonetheless. One of their main points is in defining self-referential data structures.
I can't seem to find where in julialang/julia this is actually defined, since I can't use @edit
/@less
on it
Ah there we go, they're uninitialized alright, I was just on a weird long streak of always getting zeros
Brenhin Keller has marked this topic as resolved.
Last updated: Nov 06 2024 at 04:40 UTC