<?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>Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx</link><description>Running afoul of the language rules.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10347069</link><pubDate>Thu, 06 Sep 2012 19:25:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10347069</guid><dc:creator>JamesJohnston</dc:creator><description>&lt;p&gt;Yeah I know.... it won&amp;#39;t work in a function. &amp;nbsp;I think that should work if it&amp;#39;s a class member, though. &amp;nbsp;Just the constructors need to initialize it in the initializer lists. :) &amp;nbsp;I probably should have clarified it more.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10347069" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10346991</link><pubDate>Thu, 06 Sep 2012 16:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346991</guid><dc:creator>@JamesJohnston</dc:creator><description>&lt;p&gt;Shape &amp;shape;&lt;/p&gt;
&lt;p&gt;will not compile. You cannot declare a reference without assigning to it, since, well, there is no such things as a null reference ;)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346991" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10346339</link><pubDate>Tue, 04 Sep 2012 21:30:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346339</guid><dc:creator>alegr1</dc:creator><description>&lt;p&gt;bad_cast is thrown only is you cast into a reference type. dynamic_cast to a pointer doesn&amp;#39;t throw.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346339" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10346278</link><pubDate>Tue, 04 Sep 2012 18:28:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346278</guid><dc:creator>JamesJohnston</dc:creator><description>&lt;p&gt;@POKE53280,0: &amp;nbsp;Wrong; you get a null pointer if the cast is invalid, which is then noticed by the following &amp;quot;if&amp;quot; statement.&lt;/p&gt;
&lt;p&gt;bad_cast is for references because there is no such thing as a null reference; for example:&lt;/p&gt;
&lt;p&gt;Shape &amp;shape;&lt;/p&gt;
&lt;p&gt;Shape2D &amp;amp;shape2D = dynamic_cast&amp;lt;Shape2D&amp;amp;&amp;gt;(shape); // may throw bad_cast&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346278" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10346255</link><pubDate>Tue, 04 Sep 2012 17:24:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346255</guid><dc:creator>POKE53280,0</dc:creator><description>&lt;p&gt;A simple note to Raymond&amp;#39;s code posted on mbrierst comment:&lt;/p&gt;
&lt;p&gt;[Shape2D *shape = dynamic_cast&amp;lt;Shape2D*&amp;gt;(shape);&lt;/p&gt;
&lt;p&gt;if (shape) { ... }]&lt;/p&gt;
&lt;p&gt;I think if dynamic_cast&amp;lt;&amp;gt; fails, a std::bad_cast C++ exception is thrown, so probably the simple &amp;quot;if (shape)...&amp;quot; check is not correct.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346255" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10346198</link><pubDate>Tue, 04 Sep 2012 15:03:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346198</guid><dc:creator>mbrierst</dc:creator><description>&lt;p&gt;Thanks for the cheat sheet. &amp;nbsp;Lesson learned.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346198" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10346175</link><pubDate>Tue, 04 Sep 2012 14:00:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346175</guid><dc:creator>mbrierst</dc:creator><description>&lt;p&gt;I&amp;#39;ve failed the &amp;quot;example even with dynamic_cast&amp;quot; exercise, and I don&amp;#39;t see it posted anywhere in the comments. &amp;nbsp;Does anyone have a teacher&amp;#39;s edition of this blog with this exercise completed?&lt;/p&gt;
&lt;div class="post"&gt;[&lt;i&gt;Shape2D *shape = dynamic_cast&amp;lt;Shape2D*&amp;gt;(shape);&lt;br /&gt;if (shape) { ... }&lt;br /&gt;shape-&amp;gt;Something(); /* tells the compiler that the dynamic_cast succeeded, so it can assume that shape is a Shape2D. */ -Raymond&lt;/i&gt;]&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346175" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10346067</link><pubDate>Tue, 04 Sep 2012 06:37:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346067</guid><dc:creator>Damien</dc:creator><description>&lt;p&gt;@Joshua&lt;/p&gt;
&lt;p&gt;I&amp;#39;d disagree. The Interrupt Vector Table is at address 0. Just because there&amp;#39;s no way to distinguish between &amp;quot;pointer to address 0&amp;quot; and &amp;quot;NULL pointer&amp;quot; doesn&amp;#39;t mean that the two distinct concepts should be conflated.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346067" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10346036</link><pubDate>Tue, 04 Sep 2012 01:12:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10346036</guid><dc:creator>Joshua</dc:creator><description>&lt;p&gt;@Cesar: It got famous when it was discovered that optimization was a _bad_ idea in kernel mode.&lt;/p&gt;
&lt;p&gt;If I were compiling for 32 bit nommu mode (enable 32 bit protected mode and set the segment selectors up for the famous unreal mode but stay in protected mode), I&amp;#39;d definitely disable this optimization as the interrupt vector table (which I will be accessing) is still at NULL.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10346036" width="1" height="1"&gt;</description></item><item><title>re: Adventures in undefined behavior: The premature downcast</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/31/10345196.aspx#10345953</link><pubDate>Mon, 03 Sep 2012 15:38:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10345953</guid><dc:creator>RicarDog</dc:creator><description>&lt;p&gt;@JamesJohnston Regarding dynamic_cast, VS2010 still uses the same slow method of comparing strings. I would recommend against using RTTI in general if your application&amp;#39;s performance is critical.&lt;/p&gt;
&lt;p&gt;@Barry Kelly &amp;quot;Predictability is more valuable than performance.&amp;quot; This is a case of &amp;quot;my scenario is better than yours&amp;quot;. Some applications do have high performance requirements, and the C++ language was designed with them in mind. It&amp;#39;s not an ego issue, it&amp;#39;s just that computers don&amp;#39;t have brains yet so they have to rely on yours.&lt;/p&gt;
&lt;p&gt;As a side note, please NEVER cast base to derived pointers using reinterpret_cast or C-style cast such as Shape2D* shape2d = (Shape2D*)shape, even if you have guarantee that the cast is valid, because it results in undefined behavior when the type is incomplete (i.e. Shape2D is a forward-declared type) and the compiler won&amp;#39;t complain about it. On the other hand, static_cast does generate a compile error in such cases.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10345953" width="1" height="1"&gt;</description></item></channel></rss>