# Puzzling reshape and push!

**URL:** <https://discourse.julialang.org/t/puzzling-reshape-and-push/28326>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 3, 2019, 2:42pm UTC](https://discourse.julialang.org/t/puzzling-reshape-and-push/28326 "2019-09-03T14:42:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [September 3, 2019, 2:42pm UTC](https://discourse.julialang.org/t/puzzling-reshape-and-push/28326/1 "2019-09-03T14:42:30Z")

</div>

I find the following behavior puzzling. Start with:

```julia
A = randn(5,5)
x = reshape(A, 25)

```

At this point, `A` and `x` point to the same memory block. Indeed:

```julia
x[1] = 1.0
A[1] # returns 1.0

```

But now, since `x` is one-dimensional, I can `push!` to it:

```julia
push!(x, 3)
x[1] = 0.0
A[1] # not zero

```

The `push!` decouples `x` and `A`, which now point to different blocks in memory.

But this feels like a subtle side-effect. Is this intentional? Why not preserve the guarantee that a reshaped array always points to the original array, and let `push!` raise an error instead in this situation?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 3, 2019, 3:08pm UTC](https://discourse.julialang.org/t/puzzling-reshape-and-push/28326/2 "2019-09-03T15:08:25Z")

</div>

Note that we have:

```julia
julia> xvec = rand(4);

julia> xmat = reshape(xvec, 2, 2);

julia> push!(xvec, 1.0)
ERROR: cannot resize array with shared data
Stacktrace:
 [1] _growend! at ./array.jl:811 [inlined]
 [2] push!(::Array{Float64,1}, ::Float64) at ./array.jl:853
 [3] top-level scope at REPL[50]:100:

```

I am not sure if this is related to who is “owner” of the underlying buffer but it seems kinda odd.

---

<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:** [September 3, 2019, 3:16pm UTC](https://discourse.julialang.org/t/puzzling-reshape-and-push/28326/3 "2019-09-03T15:16:00Z")

</div>

Seems worth opening an issue?

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [September 3, 2019, 3:33pm UTC](https://discourse.julialang.org/t/puzzling-reshape-and-push/28326/4 "2019-09-03T15:33:18Z")

</div>

I’m not sure what the issue here would be. I think that the behavior should be consistent, but I’m not sure how. You agree that in my example above `push!(x, 3)` should raise an error? It seems to me that an error is the consistent thing to do (especially considering @kristoffer.carlsson example).

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 3, 2019, 3:37pm UTC](https://discourse.julialang.org/t/puzzling-reshape-and-push/28326/5 "2019-09-03T15:37:06Z")

</div>

> [@e3c6](#):
>
> You agree that in my example above `push!(x, 3)` should raise an error?

It seems reasonable to error in both cases to me. It feels like an implementation detail leaking out that `push!` dealias the arrays.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [September 3, 2019, 3:41pm UTC](https://discourse.julialang.org/t/puzzling-reshape-and-push/28326/6 "2019-09-03T15:41:27Z")

</div>

I opened an issue: [https://github.com/JuliaLang/julia/issues/33143](https://github.com/JuliaLang/julia/issues/33143)
