# Access to undefined reference: But not always

**URL:** <https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441>\
**Category:** General Usage\
**Created:** [August 3, 2023, 11:20am UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441 "2023-08-03T11:20:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 3, 2023, 11:20am UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/1 "2023-08-03T11:20:19Z")

</div>

why in the case of the setindex!() function does the error not arise?  
That is, I understand why this behavior was established, but I’m curious to understand how, behind the scenes, the two situations are distinguished.

```julia
julia> v=Vector{UInt8}(undef,3)
3-element Vector{UInt8}:
 0xb0
 0x91
 0xfe

julia> vv=Vector{Vector{UInt8}}(undef,3)
3-element Vector{Vector{UInt8}}:
 #undef
 #undef
 #undef

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

julia> v[1]
0xb0

julia> v[1]=0xff
0xff

julia> vv[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(A::Vector{Vector{UInt8}}, i1::Int64)
   @ Base .\essentials.jl:13
 [2] top-level scope
   @ c:\Users\sprmn\.julia\environments\v1.9.0\array5.jl:83

julia> vv[1]=[0xff]
1-element Vector{UInt8}:
 0xff

julia> vs[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(A::Vector{String}, i1::Int64)
   @ Base .\essentials.jl:13
 [2] top-level scope
   @ c:\Users\sprmn\.julia\environments\v1.9.0\array5.jl:85

```

What are the contraindications to result, for example, , something like this

```julia
julia> vs[1]
#undef[string]

```

and to ensure that, if I use it as a parameter of the `copy(vs[1],...)` function, it doesn’t give me the error?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 3, 2023, 11:27am UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/2 "2023-08-03T11:27:20Z")

</div>

Mutable types like `Vector{Int}` cannot be stored inline in an array; their `undef` value is a special marker, notifying that there is no object stored there. For `String`, much the same is true, due to their variable size (although not being mutable).

Immutable values like `UInt8` though are stored inline, and because they are immutable and all possible bitpatterns are valid for them, they can be stored inline. An `undef` value can thus be any arbitrary value of that type.

Do note that reading from an uninitialized array is still not safe to do - this is technically undefined behavior.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 3, 2023, 12:04pm UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/3 "2023-08-03T12:04:11Z")

</div>

```julia
Thanks for the precise clarifications.

If my need is to write in that undefined location

`copyto!(vv[1],1, ...)`

how can I do?
Should location pointers be used?
which?
how?
```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 3, 2023, 12:35pm UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/4 "2023-08-03T12:35:45Z")

</div>

Writing there is fine; just regular indexing assignment will do.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 3, 2023, 12:43pm UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/5 "2023-08-03T12:43:39Z")

</div>

yes, but I can’t write “freely” inside.  
My OP’s main question was just about whether setindex() works and getindex() doesn’t.

```julia
julia> copyto!(vv[2],1,[0x2],1,1)
ERROR: UndefRefError: access to undefined reference

```

---

<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 3, 2023, 12:48pm UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/6 "2023-08-03T12:48:50Z")

</div>

Well `copyto!(vv[1], 1, ...)` will not work because there is nothing at `vv[1]`.

Youl would need to initialize `vv[1]` as follows.

```julia
julia> vv = Vector{Vector{UInt8}}(undef, 3)
3-element Vector{Vector{UInt8}}:
 #undef
 #undef
 #undef

julia> vv[1] = Vector{UInt8}(undef, 4)
4-element Vector{UInt8}:
 0xd0
 0x08
 0x2f
 0x2e

julia> vv
3-element Vector{Vector{UInt8}}:
    [0xd0, 0x08, 0x2f, 0x2e]
 #undef
 #undef

julia> copyto!(vv[1], 1:4)
4-element Vector{UInt8}:
 0x01
 0x02
 0x03
 0x04

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 3, 2023, 1:31pm UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/7 "2023-08-03T13:31:50Z")

</div>

Certain.  
In this way, as in other possible.  
But why can’t the “system” do it automatically as it does other things behind the scenes?  
something like

```julia
vv = Vector{Vector{UInt8}(undef, 4)}(undef, 3)

```

or better

```julia
vv = Vector{Vector{UInt8}(undef)}(undef, 3)

```

---

<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 3, 2023, 2:32pm UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/8 "2023-08-03T14:32:35Z")

</div>

Well Julia is not really do anything automatically.

The difference you observed in the original is that `UInt8` is a primitive. In this case, Julia let’s us look at uninitialized memory.

For mutable types that cannot be inlined, Julia has to set the “undef” type. Under the hood, this is a null pointer. When a mutable type is initialized in the array, then the pointer points to some actual place in memory representing the mutable type.

If you want to create a vector of vectors and have the inner vectors declared, then the syntax is as follows.

```julia
julia> vv = Vector{UInt8}[Vector{UInt8}(undef, 3) for i in 1:3]
3-element Vector{Vector{UInt8}}:
 [0x00, 0xff, 0xff]
 [0x20, 0x94, 0x7a]
 [0x20, 0x94, 0x7a]

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 3, 2023, 9:29pm UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/9 "2023-08-03T21:29:06Z")

</div>

Using the @edit macro I found that the two functions getindex() and setindex() are based on two different Core functions, which, first of all, do a boundary check (I took it for granted by setting the first parameter to true ) then they do the rest which in the case of getindex results in the error message.  
But the arrayref function I believe is written in C and I’m afraid it’s not “observable”

```julia
@edit vs[1]="aaa"
@edit vs[1]
Core.arrayset(true, vs, convert(String,"aaa")::String, 1)
Core.arrayref(true, vs, 1) # ERROR: UndefRefError: access to undefined reference

```

---

<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 3, 2023, 11:19pm UTC](https://discourse.julialang.org/t/access-to-undefined-reference-but-not-always/102441/10 "2023-08-03T23:19:10Z")

</div>

The relevant C code is here:

> <https://github.com/JuliaLang/julia/blob/6dd763bf2a0b130f03183e99c0ebf0a3c14c97d6/src/array.c#L548-L576>

If you really want to hack around to see how this currently works, see my package Undefs.jl:

```julia
julia> using Undefs

julia> vs = Vector{String}(undef, 5)
5-element Vector{String}:
 #undef
 #undef
 #undef
 #undef
 #undef

julia> jla_vs = Undefs.JLArrays.JLArray(vs)
Undefs.JLArrays.JLArray{Nothing}(Ptr{Nothing} @0x0000021d02771718, 0x0000000000000005, 0x9804, 0x0008, 0x00000000, 0x0000000000000005, 0x0000000000000005, nothing)

julia> unsafe_load(Ptr{Ptr{Nothing}}(jla_vs.data), 1)
Ptr{Nothing} @0x0000000000000000

julia> vs[1] = "Hello world"
"Hello world"

julia> unsafe_load(Ptr{Ptr{Nothing}}(jla_vs.data), 1)
Ptr{Nothing} @0x0000021d017152d0

julia> vs[1]
"Hello world"

julia> unsafe_string(Ptr{UInt8}(unsafe_load(Ptr{Ptr{Nothing}}(jla_vs.data), 1)+8))
"Hello world"

julia> unsafe_store!(Ptr{Ptr{Nothing}}(jla_vs.data), C_NULL)
Ptr{Ptr{Nothing}} @0x0000021d02771718

julia> vs
5-element Vector{String}:
 #undef
 #undef
 #undef
 #undef
 #undef

julia> vs[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(A::Vector{String}, i1::Int64)
   @ Base .\essentials.jl:13
 [2] top-level scope
   @ REPL[67]:1

```
