# Inbounds still gives me a bounds error

**URL:** https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281
**Category:** General Usage
**Tags:** indexing
**Created:** [May 31, 2018, 7:36am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281 "2018-05-31T07:36:19Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [May 31, 2018, 7:36am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/1 "2018-05-31T07:36:19Z")

</div>

Why does this code:

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

```

gives me an error:

```julia
ERROR: LoadError: BoundsError: attempt to access 3-element Array{Int64,1} at index [4]
Stacktrace:
 [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.:

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

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [May 31, 2018, 7:44am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/2 "2018-05-31T07:44:14Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)
#### Post date: [May 31, 2018, 7:46am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/3 "2018-05-31T07:46:23Z")

</div>

@mbauman touched upon this here [bounds checker - @inbounds propagation rules in Julia - Stack Overflow](https://stackoverflow.com/questions/38901275/inbounds-propagation-rules-in-julia) .

---

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [May 31, 2018, 7:47am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/4 "2018-05-31T07:47:29Z")

</div>

Do you mean on GitHub?

---

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [May 31, 2018, 7:49am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/5 "2018-05-31T07:49:02Z")

</div>

This

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

```

gives the same error.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [May 31, 2018, 7:54am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/6 "2018-05-31T07:54:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [May 31, 2018, 7:55am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/7 "2018-05-31T07:55:22Z")

</div>

Read the last paragraph of the stackoverflow post linked above.

---

<div class="post-metadata">

### Author: ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)
#### Post date: [May 31, 2018, 8:31am UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/8 "2018-05-31T08:31:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)
#### Post date: [May 31, 2018, 2:02pm UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/9 "2018-05-31T14:02:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 31, 2018, 4:03pm UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/10 "2018-05-31T16:03:15Z")

</div>

> [@mauro3](#):
>
> should be documented though

It should all be documented here: [https://docs.julialang.org/en/latest/devdocs/boundscheck/](https://docs.julialang.org/en/latest/devdocs/boundscheck/)

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

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [May 31, 2018, 8:26pm UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/11 "2018-05-31T20:26:36Z")

</div>

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

```julia
help?> @inbounds                                                                                                                                                         
  @inbounds(blk)                                                                                                                                                         

  Eliminates array bounds checking within expressions.

```

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

Something like:

```julia
help?> @inbounds                                                                                                                                                         
  @inbounds(blk)                                                                                                                                                         

  Eliminates array bounds checking within expressions if possible.

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 31, 2018, 9:44pm UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/12 "2018-05-31T21:44:40Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [May 31, 2018, 9:54pm UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/13 "2018-05-31T21:54:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 31, 2018, 10:00pm UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/14 "2018-05-31T22:00:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 31, 2018, 10:05pm UTC](https://discourse.julialang.org/t/inbounds-still-gives-me-a-bounds-error/11281/15 "2018-05-31T22:05:19Z")

</div>

> [@mbauman](#):
>
> Let’s throw a breadcrumb in there to `--check-bounds=yes` .

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