Stream: helpdesk (published)

Topic: ✔ Zero-initialization of objects created by `new`


view this post on Zulip Brenhin Keller (Jul 14 2022 at 04:48):

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))

view this post on Zulip Jakob Nybo Nissen (Jul 14 2022 at 04:50):

No, they are uninitialized

view this post on Zulip Michael Fiano (Jul 14 2022 at 04:50):

It's actually unspecified/not documented. Read the code, but it may change.

view this post on Zulip Michael Fiano (Jul 14 2022 at 04:52):

I would rely on them being uninitialized nonetheless. One of their main points is in defining self-referential data structures.

view this post on Zulip Brenhin Keller (Jul 14 2022 at 04:56):

I can't seem to find where in julialang/julia this is actually defined, since I can't use @edit/@less on it

view this post on Zulip Brenhin Keller (Jul 14 2022 at 05:01):

Ah there we go, they're uninitialized alright, I was just on a weird long streak of always getting zeros

view this post on Zulip Notification Bot (Jul 14 2022 at 05:01):

Brenhin Keller has marked this topic as resolved.


Last updated: Dec 28 2024 at 04:38 UTC