What does #undef mean?

Lots to unpack here, let’s start with 1 & 2:

#undef simply stands for a reference that hasn’t been specified yet. See the following example:

julia> x = Vector{Int}(undef, 3)
3-element Vector{Int64}:
 139777823454816
 139777979780016
               0

julia> y = Vector{String}(undef, 3)
3-element Vector{String}:
 #undef
 #undef
 #undef

Initializing a vector with undef means that we don’t specify its content yet.

  • In the case of an Int eltype, the vector knows the size (in bytes) of its elements and “reserves” it, but since we haven’t written anything it still contains whatever was there in memory before. Hence the seemingly random numbers, which may be different if i do it again.
  • But in the case of a String eltype, the vector doesn’t know the size of its elements, so it just stores an undef for each one.
3 Likes