<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 26, 2016 at 10:31 AM, Adrian Zubarev via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</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"><div><p>So theoretically I could build a wrapper type for <code>Unsafe(Mutable)Pointer</code> which will be safe to use and never exceed the allocated range!</p></div></div></blockquote><div>Yeah, if I remember correctly this is actually how the Swift collections are implemented. <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>

<pre><code>public func successor() -&gt; UnsafeMutablePointer&lt;Memory&gt;? {
     
    // return `nil` if out of range
}
</code></pre>

<ol>
<li>So why don’t we have safe pointers today?</li>
<li>Any technical reasons or do I miss something here?!</li>
</ol>

<p></p></div><div><span class=""><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><span style="font-family:arial,sans-serif;font-size:small;color:rgb(34,34,34)"></span></div></span></div></div></blockquote><div>The check for safety imposes a performance cost, which may or may not be okay.</div><div><br></div><div>It&#39;s also not clear sometimes exactly what &quot;out of bounds&quot; means - for example, you might have a big chunk of memory representing an array, and then you take a pointer to only part of that memory, representing a slice of the array. In this case you can write &quot;out of bounds&quot; of the slice, but the pointer type doesn&#39;t know that (because you are still within the range of the chunk of memory that you got from `UnsafeMutablePointer.memory()`). </div><div><br></div><div>The way Swift has you do it is that you can do whatever you want with pointers, but it&#39;s up to you to decide exactly what &quot;in bounds&quot; and &quot;out of bounds&quot; means, and then wrap that all up in a wrapper type with the appropriate checks. That way you can have the raw performance if you really need it, and you can have safety otherwise.</div><div> </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><span class=""><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><span style="font-family:arial,sans-serif;font-size:small;color:rgb(34,34,34)"> </span><br></div></span></div></div></blockquote><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><span class=""><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"></div> <br> <div><div style="font-family:helvetica,arial;font-size:13px">-- <br>Adrian Zubarev<br>Sent with Airmail</div></div> <br></span><div><div class="h5"><p>Am 26. Mai 2016 bei 19:14:41, Andrew Trick (<a href="mailto:atrick@apple.com" target="_blank">atrick@apple.com</a>) schrieb:</p> <blockquote type="cite"><span><div style="word-wrap:break-word"><div></div><div>






<br>
<div>
<blockquote type="cite">
<div>On May 26, 2016, at 9:59 AM, Adrian Zubarev via
swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div>
<br>
<div>
<div style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<p style="margin:15px 0px">
I’ve got one more questions about<span> </span><code style="font-family:Menlo,Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">Unsafe(Mutable)Pointer</code>. I know that I’m able to
access memory that might not belong to me.<span> </span></p>
<p style="margin:15px 0px">My question is:</p>
<ul style="margin:15px 0px">
<li style="margin:15px 0px">
<p style="margin:15px 0px">
Can I trust these functions that they will return a pointer to some
memory when I allocate more than one object AND when I’m moving
only inside that range?</p>
<div><br></div>
</li>
</ul>
</div>
</div>
</blockquote>
Yes.<br>
<blockquote type="cite">
<div>
<div style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<ul style="margin:15px 0px">
<li style="margin:15px 0px">
<pre style="margin:15px 0px;font-family:Menlo,Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(204,204,204);overflow:auto;padding:4px 8px;word-break:normal;word-wrap:normal"><code style="font-family:Menlo,Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:0px;margin:0px;padding:0px;word-break:normal;word-wrap:normal">public func successor() -&gt; UnsafeMutablePointer&lt;Memory&gt;
public func predecessor() -&gt; UnsafeMutablePointer&lt;Memory&gt;
public func advancedBy(n: Int) -&gt; UnsafeMutablePointer&lt;Memory&gt;
</code>
</pre></li>
<li style="margin:15px 0px">
<p style="margin:15px 0px">
<code style="font-family:Menlo,Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">UnsafeMutablePointer&lt;Int&gt;.alloc(4)</code><span> </span>when
I advance only in range of<span> </span><code style="font-family:Menlo,Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(234,234,234);margin:0px 2px;padding:0px 5px;word-break:normal;word-wrap:normal">[0,1,2,3]</code><span> </span>am I safe or could I get a
pointer to memory that does not belong to me?</p>
<div><br></div>
</li>
</ul>
</div>
</div>
</blockquote>
UnsafeMutablePointer&lt;T&gt;.alloc(N) creates a single object in
memory that holds N consecutive T values. Each value resides at
index*strideof(T.self) bytes beyond the allocated pointer where
index is valid in the range 0..&lt;N.</div>
<div><br></div>
<div>-Andy<br>
<blockquote type="cite">
<div>
<div style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<ul style="margin:15px 0px">
<li style="margin:15px 0px">
<p style="margin:15px 0px">
Example:</p>
<pre style="margin:15px 0px;font-family:Menlo,Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(204,204,204);overflow:auto;padding:4px 8px;word-break:normal;word-wrap:normal"><code style="font-family:Menlo,Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:0px;margin:0px;padding:0px;word-break:normal;word-wrap:normal">// imagine this is some memory portion,
// where x is memory that does not belong to me
// and 0 is moemory free to use
      
[…, x, 0, 0, 0 x, 0, x, …]
      
// now I want to allocate 4 objects   
// variant A:
      
[…, x, MY1, MY2, MY3, x, MY4, x, …]
      
// my pointer will sit at `MY1` and if I advance by 2 I&#39;ll get `x`
// can this happen to me?
      
// variant B:
// Unsafe(Mutable)Pointer will ensure that I always get memory tied together   
// (or the above functions will skip memory that doesn&#39;t belong to me??):
      
      
[…, x, MY1, MY2, MY3, MY4 x, …]   
</code>
</pre></li>
</ul>
<p style="margin:15px 0px">So which is right?</p>
<div style="margin:15px 0px"><br></div>
</div>
<div style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<div style="font-family:Helvetica,Arial;font-size:13px;margin:0px"><br></div>
<br>
<div>
<div style="font-family:helvetica,arial;font-size:13px">-- <br>
Adrian Zubarev<br>
Sent with Airmail</div>
</div>
</div>
<div style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<div style="margin:15px 0px"><br></div>
</div>
<span style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254);float:none;display:inline!important">_______________________________________________</span><br style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<span style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254);float:none;display:inline!important">swift-users mailing list</span><br style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<a href="mailto:swift-users@swift.org" style="color:rgb(65,131,196);background-color:rgb(254,254,254);text-decoration:none;font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-users@swift.org</a><br style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" style="color:rgb(65,131,196);background-color:rgb(254,254,254);text-decoration:none;font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br style="font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(254,254,254)"></div>
</blockquote>
</div>
<br>


</div></div></span></blockquote></div></div></div><div><p></p></div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>