<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx</link><description>Here's a handy type I whipped up when I was translating some complex pointer-manipulation code from C to C#. It lets you make a safe "managed pointer" to the interior of an array. You get all the operations you can do on an unmanaged pointer: you can</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10169763</link><pubDate>Tue, 31 May 2011 05:02:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10169763</guid><dc:creator>Alex Davies</dc:creator><description>&lt;p&gt;There&amp;#39;s a minor bug present in that a pointer to a zero array is permitted by the public constructor.. although this is more so yet another argument in allowing pointers to go 1 past the end ;).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10169763" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10145114</link><pubDate>Thu, 24 Mar 2011 01:02:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10145114</guid><dc:creator>Mike Strobel</dc:creator><description>&lt;p&gt;@Ben: Not safely or with particularly good performance, but you could do something like this: &lt;a rel="nofollow" target="_new" href="http://pastie.org/1706405"&gt;http://pastie.org/1706405&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Please do not use that technique for anything other than academic purposes. &amp;nbsp;It really is not a good idea.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10145114" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10144547</link><pubDate>Tue, 22 Mar 2011 21:34:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10144547</guid><dc:creator>Ben Voigt [Visual C++ MVP]</dc:creator><description>&lt;p&gt;In C and C++, taking the difference between two pointers into the same object is well-defined, whether or not that object is an array. &amp;nbsp;Is it safe to assume that there is no library implementation which would provide equivalent functionality in C#?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10144547" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10140864</link><pubDate>Mon, 14 Mar 2011 18:06:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10140864</guid><dc:creator>Mike Strobel</dc:creator><description>&lt;p&gt;Your decision to safely cast &amp;#39;x&amp;#39; as a Nullble&amp;lt;ArrayPtr&amp;lt;T&amp;gt;&amp;gt; in Equals() threw me for a moment, as &amp;#39;x&amp;#39; (typed as &amp;#39;object&amp;#39;) could not possibly be a Nullable&amp;lt;&amp;gt; (nullables lose their identity as nullables when boxed). &amp;nbsp;But this technique lets you achieve with a single &amp;#39;as&amp;#39; cast what would otherwise require both an &amp;#39;is&amp;#39; check and an unsafe cast. &amp;nbsp;Very clever.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10140864" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10140630</link><pubDate>Mon, 14 Mar 2011 11:27:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10140630</guid><dc:creator>Fede Azzato</dc:creator><description>&lt;p&gt;I believe your this[] operator should check for bound issues. Maybe something like this would be enough.&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public T this[int index]&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;get { return index == 0 ? source[this.index] : (this + index)[0]; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;set { if (index == 0) source[this.index] = value; else (this + index)[0] = value; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10140630" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10140327</link><pubDate>Sun, 13 Mar 2011 01:13:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10140327</guid><dc:creator>Navaneeth</dc:creator><description>&lt;p&gt;Nice code. A question on your programming style. Do you use assertions frequently when writing internal classes? I have heard lot of guys talking about avoid to use assertions because they clutter in the actual code. While I think assertions should be used proactively, I would like to get your view also on this. &lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10140327" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10140045</link><pubDate>Fri, 11 Mar 2011 22:02:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10140045</guid><dc:creator>Ben</dc:creator><description>&lt;p&gt;@Shuggy&lt;/p&gt;
&lt;p&gt;I agree that a guard is probably a better idea, but the beauty of allowing pointers to outside the bounds is it retains the &amp;quot;object safety&amp;quot; of the class as written. (ie, you get the assert when you try to compare an invalid value of one array to a valid value of another). I&amp;#39;d possibly go for a hybrid, now that you mention it: have all invalid values compare equal, and disallow any operation other than comparison on them. This would allow you to use invalid values as guards, keeping the safety check of &amp;quot;comparing from the same array&amp;quot;, while minimising the possiblity that you&amp;#39;ll miss seeing an invalid value misused.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10140045" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10139756</link><pubDate>Fri, 11 Mar 2011 09:28:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10139756</guid><dc:creator>Shuggy</dc:creator><description>&lt;p&gt;@Monsignor&lt;/p&gt;
&lt;p&gt;did you miss the footnote?&lt;/p&gt;
&lt;p&gt;&amp;quot;(*) Were this to be a public type then I&amp;#39;d make the assertions into exceptions because there is no telling what crazy thing the public is going to do; since this is an internal type I can guarantee that I&amp;#39;m using it correctly, so I&amp;#39;ll use an assertion instead.&amp;quot;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10139756" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10139739</link><pubDate>Fri, 11 Mar 2011 08:32:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10139739</guid><dc:creator>Graham Clark</dc:creator><description>&lt;p&gt;In your example use code, shouldn&amp;#39;t&lt;/p&gt;
&lt;p&gt; &amp;nbsp; var p5 = arr + 5;&lt;/p&gt;
&lt;p&gt;be&lt;/p&gt;
&lt;p&gt; &amp;nbsp; var p5 = p0 + 5&lt;/p&gt;
&lt;p&gt;?&lt;/p&gt;
&lt;p&gt;Otherwise it doesn&amp;#39;t compile.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10139739" width="1" height="1"&gt;</description></item><item><title>re: References and Pointers, Part Two</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx#10139722</link><pubDate>Fri, 11 Mar 2011 07:44:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10139722</guid><dc:creator>Shuggy</dc:creator><description>&lt;p&gt;@Ben&lt;/p&gt;
&lt;p&gt;Personally I believe modifying the guard (in this case to be &amp;lt;= end - 2) is appropriate. &lt;/p&gt;
&lt;p&gt;If the guard would become very complex the working out what you were going to increment/ decrement and doing a break if it would go put of bounds is also fine. &lt;/p&gt;
&lt;p&gt;I admit it might make the code slightly less easy to read but it also ensures that nobody ever has access to an invalid pointer they might use &lt;/p&gt;
&lt;p&gt;If you cared you could easily write a SafeIncrement method which returned null (or some other sentinel) once you fell off the edge and use the sentinel in your guard instead. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10139722" width="1" height="1"&gt;</description></item></channel></rss>