<div dir="ltr">Hi Jordan,<div><br></div><div>this is Swift 3 code btw, no warning / error.<div><br></div><div>you mention the same space being reused. It&#39;s self data that&#39;s being modified when mx_gels() returns, still within solve(). self storage isn&#39;t deallocated at that time, so that is not possible or I&#39;m misunderstanding something.</div></div><div><br></div><div>For the second reason you provide, I don&#39;t understand, could you please elaborate?</div><div><br></div><div>I&#39;m not against changing the code to pass inout instead, but if those really are programming errors and are not obvious neither to me, nor to the compiler, these errors will happen again.</div><div><br></div><div>Raphael</div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Aug 5, 2016 at 6:58 PM Jordan Rose &lt;<a href="mailto:jordan_rose@apple.com">jordan_rose@apple.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Those UnsafeMutablePointer&lt;T&gt;(…) calls aren’t correct. When an array is implicitly converted to a pointer in Swift, the pointer is only valid for the <i>immediate</i> call that it’s being passed to. In this case, that’s the UnsafeMutablePointer initializer, and after that the pointer is invalid. So it’s possible that either the same space is being reused for both pointers, or that it’s <i>not</i> uniquing the array because it thinks it only has to make an UnsafePointer&lt;T&gt;, or both.</div><div><br></div><div>As Brent says, passing the arrays directly to the method using inout semantics (&amp;) should get you the behavior you want.</div><div><br></div><div>(Yes, there should be a diagnostic for this. I’m pretty sure we have a bug already, so no need to file. The Swift 3 pointer APIs make this a little harder to do by accident, too.)</div><div><br></div><div>Jordan</div><div><br></div><br><div><blockquote type="cite"></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On Aug 5, 2016, at 08:14, Raphael Sebbe via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><div><div dir="ltr">Do you see a reason why the copy isn&#39;t happening in this specific case? Is it a bug, or a mistake in my code?<div><br></div><div>Thank you,</div><div><br></div><div>Raphael</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 4, 2016 at 4:18 PM Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com" target="_blank">brent@architechies.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">&gt; On Aug 4, 2016, at 1:25 AM, Raphael Sebbe via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; My understanding is that the compiler doesn&#39;t make a real copy in the acopy =  self instruction, and then provides that contents to the mx_gels_ function which modifies the memory contents.<br>
&gt;<br>
&gt;<br>
&gt; public func solve(rhs b: Matrix&lt;T&gt;) -&gt; Matrix&lt;T&gt;? {<br>
&gt;       // ...<br>
&gt;       var acopy = self<br>
&gt;       // ...<br>
&gt;<br>
&gt;       <a href="http://t.mx" target="_blank">T.mx</a>_gels_(&amp;trans, &amp;m, &amp;n, &amp;nrhs, UnsafeMutablePointer&lt;T&gt;(acopy.values), &amp;lda, UnsafeMutablePointer&lt;T&gt;(x.values), &amp;ldb, UnsafeMutablePointer&lt;T&gt;(workspace), &amp;lwork, &amp;status);<br>
&gt;<br>
&gt;       // ...<br>
&gt;       }<br>
&gt;<br>
&gt;<br>
&gt; Is this expected? I mean, I can force a real copy of course, but value semantics would suggest the code above is correct and wouldn&#39;t need that. Shouldn&#39;t the cast trigger the copy somehow? Or is there a better way of expressing this operation? Thx.<br>
<br>
The `acopy = self` line only copies the reference to the internal buffer. However, converting the array into a pointer will—or at least, if done properly, *should*—force the array to create and switch to using a unique copy of its buffer in preparation for writes through the UnsafeMutablePointer. I believe that happens here: &lt;<a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Pointer.swift#L79" rel="noreferrer" target="_blank">https://github.com/apple/swift/blob/master/stdlib/public/core/Pointer.swift#L79</a>&gt;<br>
<br>
(I say &quot;should&quot; because I&#39;m not sure you&#39;re actually creating those pointers correctly. I believe you ought to be able to just say `&amp;acopy.values` and `&amp;x.values`, and that should be a more reliable way to do it.)<br>
<br>
--<br>
Brent Royal-Gordon<br>
Architechies<br>
<br>
</blockquote></div></div></blockquote></div></div><div style="word-wrap:break-word"><div><blockquote type="cite"><div>
_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></blockquote></div>