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.
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.
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?
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> 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]
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”