Help: Passed in slice is altered even though it's a copy

polaris · · 482 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello Everybody. </p> <p>I would like to ask for some assitance here. So I know that slices point to an underlaying array, and I have this problem I worked out, for which you pass in a 2 dimensional slice and them perform some action on it. Code : <a href="https://github.com/Skarlso/goprojects/tree/master/spiralmatrix">Spiral Matrix</a></p> <p>The file is: <a href="https://github.com/Skarlso/goprojects/blob/master/spiralmatrix/spiralv3.go">spiralv3.go</a></p> <p>Now, it works perfectly fine, it&#39;s a spiral matrix calucation, never mind that. </p> <p>What I do mind is, when you call this thing in the test two times after each other the passed-in slice is modified even though I copy it into a struct. </p> <p>Now I seem to remember that copy only copies the values, which would be okay. Also, I tried it in a smaller example in playground and that worked fine. But here, It&#39;s outputting this: </p> <pre><code>[1 2 3 4 5] [6 7 8 9] [10 11 12 13] [14 15 16] [17 18 19] [20 21] [22 23] [24 25] Matrix matrix: [] spiV3: [[1 2 3 4 5] [17 18 19 19 6] [24 25 20 20 7] [23 22 21 21 8] [13 12 11 10 9]] </code></pre> <p>spiv3 is totatly scrambled. I have no idea why at this point. I tried everything, even appending, or copy-ing the values one by one to make sure, it&#39;s still getting scrambled. </p> <p>Could somebody please take a look at the source? You have more experience, I&#39;m sure I&#39;m missing something trivial here. </p> <p>Bit more explanation. </p> <p>Things are happening here: </p> <pre><code>//OrderMatrix orders a matrix func OrderMatrix(spiV3 [][]int) { m := Matrix{} m.matrix = make([][]int, len(spiV3)) copy(m.matrix, spiV3) for len(m.matrix) &gt; 0 { m.rARFirstRow() m.rARLastColumn() m.rARLastRow() m.rARFirstColumn() } fmt.Println(&#34;Matrix matrix:&#34;, m.matrix) fmt.Println(&#34;spiV3:&#34;, spiV3) } </code></pre> <p>And the output from above is partially from the two prints. Here, you can see that m.matrix is empty, since it got sliced away. But somehow spiV3 here is completely scambled and chopped as compared to the original look, which is a spiral matrix. So, yeah. I&#39;m bafled. </p> <p>Thank you!!</p> <hr/>**评论:**<br/><br/>AYBABTME: <pre><p><code>copy(m.matrix, spiV3)</code> only copies the outside slice, the inside ones are still the same. You need to copy the inner slices one by one:</p> <pre><code>dup.matrix = make([][]int, 0, len(m.matrix)) for _, in := range m.matrix { out := make([]int, len(in)) copy(out, in) dup.matrix = append(dup.matrix, out) } </code></pre></pre>skarlso: <pre><p>I swear to the Gods I tried that at some point. And bamm, this works. :) </p> <p>Dude, THANKS!! :) Indeed. I totally forgot that this way the inner slices were still references. Good stuff. Thanks man!</p></pre>boomshroom: <pre><p>There is a proposal to add <a href="https://github.com/golang/go/issues/13253" rel="nofollow">&#34;strided&#34; (multidimensional) slices</a> that would fix this problem.</p></pre>skarlso: <pre><p>It does sound interesting. It is still in large discussion. I need to dig in deeper to understand everything what&#39;s been proposed. :) But thanks for the link!!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

482 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传