<div dir="ltr">Thanks, +1 for rewriting it like that.<div>/Jens</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 5, 2016 at 9:58 PM, Andrew Trick <span dir="ltr"><<a href="mailto:atrick@apple.com" target="_blank">atrick@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><br><div><blockquote type="cite"><div>On Aug 5, 2016, at 12:43 PM, Jens Persson <<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>> wrote:</div><br><div><div dir="ltr"><div data-smartmail="gmail_signature">I'm trying to understand the new Swift 3 (4?) pointer API and Swift's memory model.</div><div data-smartmail="gmail_signature"><br></div><div data-smartmail="gmail_signature">More specifically, I'd like to know more about what exactly it means for a pointer to be initialized or not.</div><div data-smartmail="gmail_signature"><br></div><div data-smartmail="gmail_signature"><div data-smartmail="gmail_signature">For example, I suppose the following code example doesn't satisfy the precondition in the subscript documentation (ie floatsPtr not being initialized when using its subscript):</div><div data-smartmail="gmail_signature"><br></div><div data-smartmail="gmail_signature">let numFloats = 123</div><div data-smartmail="gmail_signature">let floatsPtr = UnsafeMutablePointer<Float>.<wbr>allocate(capacity: numFloats)</div><div data-smartmail="gmail_signature">for i in 0 ..< numFloats { floatsPtr[i] = Float(i) * 0.1 } // Setting values</div><div data-smartmail="gmail_signature">for i in 0 ..< numFloats { print(floatsPtr[i]) } // Getting values</div><div data-smartmail="gmail_signature">floatsPtr.deallocate(capacity: numFloats)</div><div data-smartmail="gmail_signature"><br></div><div data-smartmail="gmail_signature">I'd like to understand why/how this could lead to undefined behavior, and what exactly it means for a pointer to be initialized or not.</div><div><br></div><div><div>I've read <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md" target="_blank">https://github.com/apple/<wbr>swift-evolution/blob/master/<wbr>proposals/0107-<wbr>unsaferawpointer.md</a></div></div><div><br></div><div>But I don't feel that I fully understand what it means for a pointer to be initialized, or bound, and if the preconditions and rules for undef behavior are the same no matter if Pointee is a trivial type or a class type. </div></div></div></div></blockquote><br></div></span><div>I think it’s common practice to initialize trivial types via subscript assignment. Earlier versions of the proposal actually showed examples of this and claimed that it was valid pattern. However, during review those examples were removed because it encouraged bad practice and complicated the issue.</div><div><br></div><div>The fact is, code like this is not going to break anything in the compiler and it’s common enough that any model model verifier is going to need to special-case trivial types. I think it would be fine to rewrite the subscript precondition as follows:</div><div><br></div><div><div>/// - Precondition: the pointee at `self + i` is initialized.</div><div>should read</div><div>/// - Precondition: either the pointee at `self + i` is initialized</div><div>/// or `Pointee` is a trivial type.</div></div><div><br></div><div><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md#trivial-types" target="_blank">https://github.com/apple/<wbr>swift-evolution/blob/master/<wbr>proposals/0107-<wbr>unsaferawpointer.md#trivial-<wbr>types</a></div><div><br></div><div>-Andy</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">bitCycle AB | Smedjegatan 12 | 742 32 Östhammar | Sweden<br><a href="http://www.bitcycle.com/" target="_blank">http://www.bitcycle.com/</a><br>Phone: +46-73-753 24 62<br>E-mail: <a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a><br><br></div>
</div>