<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; a=zeros(1,1); b = reshape(a,1); push!(b,1); b[1]=-1; a,b
([0.0], [-1.0, 1.0])"><pre class="notranslate"><code class="notranslate">julia&gt; a=zeros(1,1); b = reshape(a,1); push!(b,1); b[1]=-1; a,b
([0.0], [-1.0, 1.0])
</code></pre></div>
<p dir="auto">Expected behavior:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; push!(b,1)
ERROR: cannot resize array with shared data"><pre class="notranslate"><code class="notranslate">julia&gt; push!(b,1)
ERROR: cannot resize array with shared data
</code></pre></div>
<p dir="auto">Not really sure whether this is a bug or intended, but it is surprising and weird (I would have expected reshape to lock further resizing on either both the source and the target, or on neither).</p>
<p dir="auto">Consider the following example:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="xmat = randn(5,5)
xvec = reshape(xmat, 25)
push!(xvec, 3)
xvec[1] = 0.0
xmat[1] # not zero"><pre class="notranslate">xmat <span class="pl-k">=</span> <span class="pl-c1">randn</span>(<span class="pl-c1">5</span>,<span class="pl-c1">5</span>)
xvec <span class="pl-k">=</span> <span class="pl-c1">reshape</span>(xmat, <span class="pl-c1">25</span>)
<span class="pl-c1">push!</span>(xvec, <span class="pl-c1">3</span>)
xvec[<span class="pl-c1">1</span>] <span class="pl-k">=</span> <span class="pl-c1">0.0</span>
xmat[<span class="pl-c1">1</span>] <span class="pl-c"><span class="pl-c">#</span> not zero</span></pre></div>
<p dir="auto">Here <code class="notranslate">push!</code> has decoupled <code class="notranslate">xmat</code> and <code class="notranslate">xvec</code>, though one would expect that a reshaped array always point to the same memory as the original array. I think the <code class="notranslate">push!</code> here should raise an error.</p>
<p dir="auto">Contrast the example above with the following (source: <a href="https://discourse.julialang.org/t/puzzling-reshape-and-push/28326/2" rel="nofollow">https://discourse.julialang.org/t/puzzling-reshape-and-push/28326/2</a>):</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="xvec = rand(4)
xmat = reshape(xvec, 2, 2)
push!(xvec, 1.0)  # ERROR: cannot resize array with shared data"><pre class="notranslate">xvec <span class="pl-k">=</span> <span class="pl-c1">rand</span>(<span class="pl-c1">4</span>)
xmat <span class="pl-k">=</span> <span class="pl-c1">reshape</span>(xvec, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>)
<span class="pl-c1">push!</span>(xvec, <span class="pl-c1">1.0</span>)  <span class="pl-c"><span class="pl-c">#</span> ERROR: cannot resize array with shared data</span></pre></div>
<p dir="auto">To be consistent it seems to me that my first example above should also be an error.</p>