# Initializing Array of Arrays with undef gives UndefRefError?

**URL:** https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266
**Category:** General Usage
**Tags:** arrays
**Created:** [January 13, 2021, 3:46am UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266 "2021-01-13T03:46:39Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![cosmia](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cosmia](https://discourse.julialang.org/u/cosmia)
#### Post date: [January 13, 2021, 3:46am UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/1 "2021-01-13T03:46:39Z")

</div>

Hi all,

I am initializing an Array of Arrays with undef. Why does it give me an UndefRefError when accessing it, whereas an Array with undef does not? Also, did some behavior change? I seem to remember initializing Array of Arrays before and never having a UndefRefError before.

```julia
a = Array{Int64}(undef, 10)
b = Array{Array{Int64}}(undef, 10)

a[1] # No error
b[1] # UndefRefError

```

PS: performance issues or best practices asides. It seems that using `fill` to initialize Array of Arrays might be better, but I’m just wondering what’s happening in the above case.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [January 13, 2021, 4:32am UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/2 "2021-01-13T04:32:40Z")

</div>

When you use `Vector{T}(undef, n)`, the uninitialized elements of the vector will be filled with random data (whatever happens to be in memory) if `T` is an `isbits` type. If `T` is not an `isbits` type, then you will get an `UndefRefError` if you try to access an uninitialized element, since the element hasn’t been initialized with a pointer to an instance of the non-`isbits` type.

```julia
julia> isbitstype(Int)
true

julia> Vector{Int}(undef, 1)
1-element Array{Int64,1}:
 4588932080

julia> isbitstype(String)
false

julia> Vector{String}(undef, 1)
1-element Array{String,1}:
 #undef

julia> struct A
           x::Int
       end

julia> isbitstype(A)
true

julia> Vector{A}(undef, 1)
1-element Array{A,1}:
 A(4438511664)

julia> mutable struct B
           x::Int
       end

julia> isbitstype(B)
false

julia> Vector{B}(undef, 1)
1-element Array{B,1}:
 #undef

```

---

<div class="post-metadata">

### Author: ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)
#### Post date: [January 13, 2021, 8:27am UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/3 "2021-01-13T08:27:25Z")

</div>

> It seems that using `fill` to initialize Array of Arrays might be better

Watch out for the potential pitfall. See the example in [Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Base.fill)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [January 13, 2021, 12:40pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/4 "2021-01-13T12:40:32Z")

</div>

> [@cosmia](#):
>
> It seems that using `fill` to initialize Array of Arrays might be better

As @Paul_Soderlind mentioned, `fill` is probably not the right tool, unless you want every field to have the _same_ vector. An alternative is using a list comprehension:

```julia
b = [Vector{Int64}() for _ = 1:10]

```

But there is nothing wrong with creating a uninitialized `Vector` and then assign a inner `Vector` to each position before accessing it.

---

<div class="post-metadata">

### Author: ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)
#### Post date: [September 3, 2021, 1:47pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/5 "2021-09-03T13:47:29Z")

</div>

> [@Henrique\_Becker](#):
>
> But there is nothing wrong with creating a uninitialized `Vector` and then assign a inner `Vector` to each position before accessing it.

Can you tell me how to do this? I’ve created an array of vectors and trying to assign value to one of them but getting the same error as above.

```julia
z= Array{Vector{Float64}}(undef, 10, 100)
z[1][1] = [1.,2.,3.]

```

which gives the error.

```julia
UndefRefError: access to undefined reference

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [September 3, 2021, 1:51pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/6 "2021-09-03T13:51:27Z")

</div>

```julia
julia> z = Array{Vector{Float64}}(undef, 10, 100);

julia> z[1] = [1., 2., 3.]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

```

---

<div class="post-metadata">

### Author: ![Minimum-Pollution-96](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Minimum-Pollution-96](https://discourse.julialang.org/u/Minimum-Pollution-96)
#### Post date: [September 3, 2021, 1:54pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/7 "2021-09-03T13:54:15Z")

</div>

Thanks! I should have written `z[1,1]`!

---

<div class="post-metadata">

### Author: ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)
#### Post date: [August 23, 2022, 2:41pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/8 "2022-08-23T14:41:26Z")

</div>

Experimenting with 3d and mutable struct (notisbits)

```julia
 julia> mtx = Matrix{Vector{SomeStruct}}(undef, 3, 3)
3×3 Matrix{Vector{SomeStruct}}:
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef

julia> mtx[1]
ERROR: UndefRefError: access to undefined reference

```

What’s the solution?

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [August 23, 2022, 2:45pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/9 "2022-08-23T14:45:55Z")

</div>

What did you expect to happen there?

---

<div class="post-metadata">

### Author: ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)
#### Post date: [August 23, 2022, 2:47pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/10 "2022-08-23T14:47:33Z")

</div>

I want to do

```julia
julia> push!(mtx[1], SomeStruct(1)) 

```

But this throws the UndefRefError too

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 23, 2022, 2:58pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/11 "2022-08-23T14:58:33Z")

</div>

You need to first initialize the array at every position:

```julia
julia> struct SomeStruct x::Int end

julia> mtx = Matrix{Vector{SomeStruct}}(undef, 3, 3);

julia> for i in eachindex(mtx)
           mtx[i] = SomeStruct[]
       end

julia> push!(mtx[1], SomeStruct(1))
1-element Vector{SomeStruct}:
 SomeStruct(1)

```

This is more succinct:

```julia
julia> mtx = [SomeStruct[] for _ in 1:3, _ in 1:3 ]
3×3 Matrix{Vector{SomeStruct}}:
 [] [] []
 [] [] []
 [] [] []

julia> push!(mtx[1], SomeStruct(1))
1-element Vector{SomeStruct}:
 SomeStruct(1)

julia> mtx
3×3 Matrix{Vector{SomeStruct}}:
 [SomeStruct(1)] [] []
 [] [] []
 [] [] []

```

---

<div class="post-metadata">

### Author: ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)
#### Post date: [August 23, 2022, 3:02pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/12 "2022-08-23T15:02:37Z")

</div>

Thanks a lot. Was trying to avoid loops looks like no way.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [August 23, 2022, 3:14pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/13 "2022-08-23T15:14:24Z")

</div>

> [@Tharsalys](#):
>
> Was trying to avoid loops looks like no way

I’m curious why you were trying to avoid loops. This is a common heuristic from programmers coming from other languages since loops in those languages may be slow. Often in Julia, a properly written loop is often the fastest approach.

Given the criteria of no loops, we can use `map`:

```julia
julia> map(_->SomeStruct[], CartesianIndices((3,3)))
3×3 Matrix{Vector{SomeStruct}}:
 [] [] []
 [] [] []
 [] [] []

```

This is essentially the same as the list comprehension above

---

<div class="post-metadata">

### Author: ![Tharsalys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tharsalys/32/38414_2.png) [@Tharsalys](https://discourse.julialang.org/u/Tharsalys)
#### Post date: [August 23, 2022, 4:53pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/14 "2022-08-23T16:53:05Z")

</div>

Purely for readability purposes, no other reason. Initializing a matrix (only once) doesn’t have to be too efficient, just readable so that was why. I agree tho, when it comes to performance I’ve found loops generally perform much better than `map` et al.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 23, 2022, 5:15pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/15 "2022-08-23T17:15:00Z")

</div>

> [@Tharsalys](#):
>
> Purely for readability purposes

This also works (combining previous suggestions):

```julia
julia> [SomeStruct[] for _ in CartesianIndices((3,3)) ]
3×3 Matrix{Vector{SomeStruct}}:
 [] [] []
 [] [] []
 [] [] []

```

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [August 23, 2022, 5:55pm UTC](https://discourse.julialang.org/t/initializing-array-of-arrays-with-undef-gives-undefreferror/53266/16 "2022-08-23T17:55:02Z")

</div>

There really is not much practical difference thanks to the compiler. This is almost purely stylistic.

```julia
julia> f() = map(_->SomeStruct[], CartesianIndices((3,3)))
f (generic function with 1 method)

julia> g() = [SomeStruct[] for _ in CartesianIndices((3,3)) ]
g (generic function with 1 method)

julia> @btime f()
  239.900 ns (10 allocations: 560 bytes)
3×3 Matrix{Vector{SomeStruct}}:
 [] [] []
 [] [] []
 [] [] []

julia> @btime g()
  239.467 ns (10 allocations: 560 bytes)
3×3 Matrix{Vector{SomeStruct}}:
 [] [] []
 [] [] []
 [] [] []

```
