Inbounds still gives me a bounds error

Why does this code:

T = [1,2,3]
@inbounds x = T[4] 

gives me an error:

ERROR: LoadError: BoundsError: attempt to access 3-element Array{Int64,1} at index [4]
 [1] getindex(::Array{Int64,1}, ::Int64) at ./array.jl:554
 [2] include_string(::String, ::String) at ./loading.jl:522
 [3] include_string(::String, ::String, ::Int64) at /home/gluon/.julia/v0.6/CodeTools/src/eval.jl:30
 [4] include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N} where N) at /home/gluon/.julia/v0.6/CodeTools/src/eval.jl:34
 [5] (::Atom.##92#97{String,Int64,String})() at /home/gluon/.julia/v0.6/Atom/src/eval.jl:52
 [6] withpath(::Atom.##92#97{String,Int64,String}, ::String) at /home/gluon/.julia/v0.6/CodeTools/src/utils.jl:30
 [7] withpath(::Function, ::String) at /home/gluon/.julia/v0.6/Atom/src/eval.jl:38
 [8] hideprompt(::Atom.##91#96{String,Int64,String}) at /home/gluon/.julia/v0.6/Atom/src/repl.jl:67
 [9] macro expansion at /home/gluon/.julia/v0.6/Atom/src/eval.jl:49 [inlined]
 [10] (::Atom.##90#95{Dict{String,Any}})() at ./task.jl:80
while loading /media/DATA/PhD/code/ising/ising_julia_code/Lattice2.jl, in expression starting on line 24

And just in case, is it possible to use @inboudns and @view in a sum call, like e. g.:

sum(@inbounds @view A[1:end, 2])

Looks like this only works inside a function (should be documented though, can you make a PR?):

julia> function f()
       T= [1,2,3]
       @inbounds x = T[4]
f (generic function with 1 method)                                                                                                                                      
julia> f()                                                                                                                                                              
140194507264240                 # garbage                                                                                                                                        
1 Like

@mbauman touched upon this here .


Do you mean on GitHub?


T = [1,2,3]
function f()
	@inbounds x = T[4] 

gives the same error.

Doing const T=[1,2,3] works.

Read the last paragraph of the stackoverflow post linked above.

Oh yeah, thanks. Was too sloppy in reading the article.

Please note that accessing an out-of-bounds index with an @inbounds declaration is undefined behavior and may cause any arbitrary behavior, including launching nukes, throwing arbitrary non-sensical errors, returning garbage, crashing or calling your parents to complain that you’ve been bad.


It should all be documented here:

As usual, though, those reading the docs for the first time are often the ones in the best place to suggest improvements!


Maybe a qualification in the doc-string would be good. Currently it says

help?> @inbounds                                                                                                                                                         

  Eliminates array bounds checking within expressions.

which suggests to me that the bounds checks are indeed always eliminated.

Something like:

help?> @inbounds                                                                                                                                                         

  Eliminates array bounds checking within expressions if possible.
1 Like

That’s a great idea — the docs I linked are indeed devdocs. We could definitely do for a bit more information in the user-facing docstring.

Suggested wording:

Grant the compiler permission to skip bounds checks within the given expression by simply assuming that all indexing is in bounds. If the given index is not actually in bounds your program may crash or produce complete garbage. Elimination of bounds checks is not guaranteed by the @inbounds annotation, merely permitted.


Let’s throw a breadcrumb in there to --check-bounds=yes. Otherwise that looks great.


And an example! The docstring for @boundscheck have a good one that should be applicable here too.

1 Like