# @inbounds not working?

**URL:** <https://discourse.julialang.org/t/inbounds-not-working/78033>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 17, 2022, 3:56pm UTC](https://discourse.julialang.org/t/inbounds-not-working/78033 "2022-03-17T15:56:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [March 17, 2022, 3:56pm UTC](https://discourse.julialang.org/t/inbounds-not-working/78033/1 "2022-03-17T15:56:58Z")

</div>

Simply tried the following:

```julia
a=[0.5]
@inbounds b=a[2]

```

yielding surprisingly

```julia
BoundsError: attempt to access 1-element Vector{Float64} at index [2]

Stacktrace:
 [1] getindex(A::Vector{Float64}, i1::Int64)
   @ Base .\array.jl:805
 [2] top-level scope
   @ In[9]:2
 [3] eval
   @ .\boot.jl:360 [inlined]
 [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1116

```

Have I missed some basic point?

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [March 17, 2022, 4:09pm UTC](https://discourse.julialang.org/t/inbounds-not-working/78033/2 "2022-03-17T16:09:02Z")

</div>

> [@photor](#):
>
> ```julia
> a=[0.5]
> @inbounds b=a[2]
> 
> ```

May be because you’re working in the global scope?

```julia
julia> function ff()
       a = [0.5]
       @inbounds b = a[2]
       end
ff (generic function with 1 method)

julia> ff()
1.32235279e-315

```

---

<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:** [March 17, 2022, 4:45pm UTC](https://discourse.julialang.org/t/inbounds-not-working/78033/3 "2022-03-17T16:45:50Z")

</div>

It is somewhat more subtle than that:

```julia
julia> x = [1]
1-element Vector{Int64}:
 1

julia> let a = x
           @inbounds b = a[2]
       end
ERROR: BoundsError: attempt to access 1-element Vector{Int64} at index [2]
Stacktrace:
 [1] getindex(A::Vector{Int64}, i1::Int64)
   @ Base ./array.jl:861
 [2] top-level scope
   @ REPL[8]:2

```

---

<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:** [March 17, 2022, 4:50pm UTC](https://discourse.julialang.org/t/inbounds-not-working/78033/4 "2022-03-17T16:50:50Z")

</div>

To be specific, `@inbounds` only works inside type-stable compiled contexts with inlining enabled. If you’re not compiled or type-stable, then the overhead from bounds checking is the least of your worries.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 17, 2022, 6:07pm UTC](https://discourse.julialang.org/t/inbounds-not-working/78033/5 "2022-03-17T18:07:38Z")

</div>

It’s worth pointing out that this overhead is pretty negligible in general. The only time it’s likely to make much of a difference is when it prevents SIMD.
