<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's self data that's being modified when mx_gels() returns, still within solve(). self storage isn't deallocated at that time, so that is not possible or I'm misunderstanding something.</div></div><div><br></div><div>For the second reason you provide, I don't understand, could you please elaborate?</div><div><br></div><div>I'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 <<a href="mailto:jordan_rose@apple.com">jordan_rose@apple.com</a>> 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<T>(…) 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<T>, or both.</div><div><br></div><div>As Brent says, passing the arrays directly to the method using inout semantics (&) 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 <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> 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'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 <<a href="mailto:brent@architechies.com" target="_blank">brent@architechies.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> On Aug 4, 2016, at 1:25 AM, Raphael Sebbe via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:<br>
><br>
> My understanding is that the compiler doesn'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>
><br>
><br>
> public func solve(rhs b: Matrix<T>) -> Matrix<T>? {<br>
> // ...<br>
> var acopy = self<br>
> // ...<br>
><br>
> <a href="http://t.mx" target="_blank">T.mx</a>_gels_(&trans, &m, &n, &nrhs, UnsafeMutablePointer<T>(acopy.values), &lda, UnsafeMutablePointer<T>(x.values), &ldb, UnsafeMutablePointer<T>(workspace), &lwork, &status);<br>
><br>
> // ...<br>
> }<br>
><br>
><br>
> 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't need that. Shouldn'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: <<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>><br>
<br>
(I say "should" because I'm not sure you're actually creating those pointers correctly. I believe you ought to be able to just say `&acopy.values` and `&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>