<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">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>
> T.mx_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>