# View bounds checking

**URL:** <https://discourse.julialang.org/t/view-bounds-checking/393>\
**Category:** General Usage\
**Created:** [November 17, 2016, 7:10pm UTC](https://discourse.julialang.org/t/view-bounds-checking/393 "2016-11-17T19:10:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 17, 2016, 7:10pm UTC](https://discourse.julialang.org/t/view-bounds-checking/393/1 "2016-11-17T19:10:17Z")

</div>

View’s docstring:

> view(A, inds…)  
> Like getindex, but returns a view into the parent array A with the given indices instead of making a copy. Calling getindex or setindex! on the returned SubArray computes the indices to the parent array on the fly without checking bounds.

However, bounds are checked in practice:

```julia
view([1,2,3], 1:2)[3] # triggers an error

```

Am I reading it right? It seems inconsistent.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 17, 2016, 7:29pm UTC](https://discourse.julialang.org/t/view-bounds-checking/393/2 "2016-11-17T19:29:43Z")

</div>

You did a OOB access on the index not the array. The kind of undefined behavior described by the doc (and should never be used in real code) is

```julia
julia> idx = [1, 2]
2-element Array{Int64,1}:
 1
 2

julia> subary = view([1, 2, 3], idx)
2-element SubArray{Int64,1,Array{Int64,1},Tuple{Array{Int64,1}},false}:
 1
 2

julia> subary[2]
2

julia> idx[2] = 3
3

julia> subary[2]
3

julia> idx[2] = 4
4

julia> subary[2]
10

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 17, 2016, 8:10pm UTC](https://discourse.julialang.org/t/view-bounds-checking/393/3 "2016-11-17T20:10:03Z")

</div>

That makes sense. `view([1, 2, 3], [1,4])` triggers an error, so it seems that the main issue is modifying the index array afterwards.

---

<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:** [November 17, 2016, 8:50pm UTC](https://discourse.julialang.org/t/view-bounds-checking/393/4 "2016-11-17T20:50:03Z")

</div>

That warning could probably be worded a bit better. I believe it was originally written with the meaning that @cstjean first understood, but it’s ambiguous since there are two places where bounds checks can occur. Prior to 0.5, SubArrays themselves didn’t check bounds on indexing, but the parent array would still check bounds on the computed access.

In 0.5, that has now flipped. Views now check bounds upon indexing to ensure that accesses are within their `size`, but then they access the parent array with bounds checks turned off.
