# Problem placing views into an array

**URL:** <https://discourse.julialang.org/t/problem-placing-views-into-an-array/60118>\
**Category:** General Usage\
**Tags:** question, array, views\
**Created:** [April 27, 2021, 5:06pm UTC](https://discourse.julialang.org/t/problem-placing-views-into-an-array/60118 "2021-04-27T17:06:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Based](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/based/32/24463_2.png) [@Based](https://discourse.julialang.org/u/Based)\
**Post date:** [April 27, 2021, 5:06pm UTC](https://discourse.julialang.org/t/problem-placing-views-into-an-array/60118/1 "2021-04-27T17:06:59Z")

</div>

When I add a `view` into an array, it creates a copy of it instead of creating a reference to the original `view`. Modifying the `view` does not modify the array it was added to. This is not the case when the array is not a view. It also seems to depend on the type of the array, `Any` seems to work.

```julia
a=[[1,2,3]]
b=[3,4,5,6]
c=view(b, 2:4)
push!(a,c)
c[3]=1000
@show a
@show c
@show a[2] == c

#result
a = [[1, 2, 3], [4, 5, 6]]
c = [4, 5, 1000]
a[2] == c = false

#desired result
a = [[1, 2, 3], [4, 5, 1000]]
c = [4, 5, 1000]
a[2] == c = true

```

Is there any way to make this work? I would like to have views so I don’t have to assign new memory or copy arrays around all the time.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 27, 2021, 5:10pm UTC](https://discourse.julialang.org/t/problem-placing-views-into-an-array/60118/2 "2021-04-27T17:10:31Z")

</div>

The problem is that `a` is a `Vector{Vector{Float64}}` so it can’t store views. This would work if you defined `a=AbstractVector{Float64}[[1,2,3]]`, but this will likely cause performance issues since it will introduce type instability.

---

<div class="post-metadata">

**Author:** ![Based](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/based/32/24463_2.png) [@Based](https://discourse.julialang.org/u/Based)\
**Post date:** [April 29, 2021, 4:48am UTC](https://discourse.julialang.org/t/problem-placing-views-into-an-array/60118/3 "2021-04-29T04:48:52Z")

</div>

Thank you. I didn’t even think to try typeof(c) which is `SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}`, and creating an array for this type seems to work fine.

I had in my head that views were just like pointers to the middle of another array like in C, which is all I am using them for. Is there any way to achieve that behavior?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 29, 2021, 4:58am UTC](https://discourse.julialang.org/t/problem-placing-views-into-an-array/60118/4 "2021-04-29T04:58:57Z")

</div>

Short answer is no. Julia arrays store their size, so a pointer to a location in an array is not an Array.
