<div dir="ltr"><div class="gmail_signature" data-smartmail="gmail_signature">I&#39;m trying to understand the new Swift 3 (4?) pointer API and Swift&#39;s memory model.</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">More specifically, I&#39;d like to know more about what exactly it means for a pointer to be initialized or not.</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature"><div class="gmail_signature" data-smartmail="gmail_signature">For example, I suppose the following code example doesn&#39;t satisfy the precondition in the subscript documentation (ie floatsPtr not being initialized when using its subscript):</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">let numFloats = 123</div><div class="gmail_signature" data-smartmail="gmail_signature">let floatsPtr = UnsafeMutablePointer&lt;Float&gt;.allocate(capacity: numFloats)</div><div class="gmail_signature" data-smartmail="gmail_signature">for i in 0 ..&lt; numFloats { floatsPtr[i] = Float(i) * 0.1 } // Setting values</div><div class="gmail_signature" data-smartmail="gmail_signature">for i in 0 ..&lt; numFloats { print(floatsPtr[i]) } // Getting values</div><div class="gmail_signature" data-smartmail="gmail_signature">floatsPtr.deallocate(capacity: numFloats)</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">I&#39;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&#39;ve read <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md">https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md</a></div></div><div><br></div><div>But I don&#39;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><br></div><div>/Jens</div><div><br></div></div>
</div>