<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Foognostic blogs &#187; c++</title>
	<atom:link href="http://blogs.foognostic.net/topics/code/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.foognostic.net</link>
	<description>Seeking knowledge of foo</description>
	<lastBuildDate>Fri, 23 Apr 2010 12:45:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>C++ versus Java, or references to references versus values of references.</title>
		<link>http://blogs.foognostic.net/2008/12/cpp-versus-java-or-references-to-references-versus-values-of-references/</link>
		<comments>http://blogs.foognostic.net/2008/12/cpp-versus-java-or-references-to-references-versus-values-of-references/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 02:36:21 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[c]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=24</guid>
		<description><![CDATA[C++ has a more useful family of reference types than Java.]]></description>
			<content:encoded><![CDATA[<p>A few years of Java really make it hard to write C++ again. Quick, which language is this?</p>

<p>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;stuff.yodel();</div></div>

</p>

<p>That works in Java and C++ but for different reasons. It's usually safer in C++.</p>

<p>In Java that code is less safe because it hasn't been tested for null. That is, one of the following already happened:
      <ol type="A">
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff stuff = null;</div></div>

</li>
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff stuff = new Stuff();</div></div>

</li>
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff stuff = Stuff.class.newInstance();</div></div>

</li>
      </ol>
      The guarantee is that each reference will have an intentional value, which <b>is</b> more than C++ promises for pointer values.
</p>

<p>C++ gives you stronger references. For that same line of code, one of these already happened:
      <ol type="A">
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff stuff;</div></div>

</li>
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff *stuff_p = new Stuff(), &amp;amp;stuff = *stuff_p;</div></div>

</li>
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff&amp;amp; f2() { Stuff stuff; return stuff; }</div></div>

</li>
      </ol>
      Which means...
      <ol type="A">
        <li>This

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stuff</div></div>

refers to an object allocated on the stack. It's there, baby -- as much as any

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">int</div></div>

or

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">float</div></div>

. 
        <li>This

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stuff</div></div>

refers to an object successfully allocated in the heap.
        <li><b>Very bad.</b>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">f2</div></div>

destructed the stuff before giving it to you. Just keep compiler warnings on and it will bark &quot;warning: reference to local variable 'stuff' returned&quot;
      </ol>
</p>

<p>What wasn't shown for C++ was

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">stuff-&amp;gt;yodel()</div></div>

. That followed one of:
      <ol type="A">
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff *stuff;</div></div>

</li>
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff *stuff = null;</div></div>

</li>
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Stuff *stuff = new Stuff();</div></div>

</li>
        <li>

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">std::auto_ptr&amp;lt;Stuff&amp;gt; stuff(new Stuff());</div></div>

</li>
      </ol>
      Which means...
      <ol type="A">
        <li>Uninitialized pointer, a.k.a. <i>Runtime Russian Roulette</i>. Unfortunately I couldn't get g++ to whine about this even with

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-Wall -Wpedantic -Wextra</div></div>

.</li>
        <li>Null pointer, a.k.a. the minimum guarantee of Java.
        <li>Hmm, I don't <i>see</i> a delete any where around here... uh-oh...
        <li>There you go. You've got a smart pointer on the stack to prevent memory leaks, while preserving the expected

<div class="codecolorer-container text vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-&amp;gt;</div></div>

syntax.
      </ol>
</p>

<h2>So WHAT? Really now!</h2>

<p>Java guarantees that a reference will be initialized, and that it must always be tested for null. C++ warms the hearts of bearded old curmudgeons with more types of references. Each type is useful alone and in sometimes in conjunction (e.g. non-const references to pointers for output parameters). This is useful and important information to know. Draping Object everywhere and automating garbage collection is a lowbrow kludge.</p>

<p><b>Footnote 1</b>: Groovy's <a href="http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29">safe navigation operator</a> helps test for null by GOTOing the next safe expression. An interesting idea, and hopefully self-preventative. If I saw code with lots of '?' it would be up for a rewrite sooner rather than later.
</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2008/12/cpp-versus-java-or-references-to-references-versus-values-of-references/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Resolving to use C++ in 2009</title>
		<link>http://blogs.foognostic.net/2008/12/resolving-to-use-cpp-in-2009/</link>
		<comments>http://blogs.foognostic.net/2008/12/resolving-to-use-cpp-in-2009/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 21:01:22 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[c]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=23</guid>
		<description><![CDATA[2009 is almost here and it's time to start thinking about resolutions. Everyone knows that resolutions are honored more in the breach than the observance. I kinda sorta resolve to use C++ at home at least once in 2009.]]></description>
			<content:encoded><![CDATA[<p>2009 is almost here and it's time to start thinking about resolutions. Everyone knows that resolutions are honored more in the breach than the observance. Keeping that in mind, here are my presolutions for 2009:</p>

<h2>A.) I hereby resolve that 2009 will be the year of C++ for my coding hobby.</h2>

<p> 
... and ... </p>

<h6>B.) I resolve to forget about this resolution as soon as my attention span wanes.</h6>

<p>I'm justifying this lack of commitment by calling these pre-resolutions or "presolutions" for short. It's only December 2008, I don't see any problems with that.</p>

<p>I haven't used C++ since 2005; back then exceptions and <a href="http://www.boost.org/">Boost</a> were verboten in the name of cross-platform compatibility. It was a little annoying but understandable; we had to appease C++ compilers from Microsoft, IBM, Sun, GNU, Metrowerks, and others. Hacks appeared all the time... sometimes you just wanted to ignore code rejected by the excellent <a href="http://www.comeaucomputing.com/tryitout/">Comeau online C++ compiler</a>. But actually shipping things means living with warts sometimes.</p>

<p>Back in 2005 I had trouble imagining that Java would survive. I watched the misbegotten offspring of C++ and Visual Basic nearly reimplement CORBA in EJB2 and cackled in glee. Demise was surely imminent.</p>

<p>I've been using Java almost exclusively since 2005. Joke's on me!!</p>

<p>Since then I have been afraid that C++ was fading away into obsolescence. <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">TIOBE</a> says I'm wrong. All those trendy important new programs like "web browsers" not implemented in dynamic languages seem to concur.</p>

<p>Yes, these demonstrate my complete inability to predict trends in my occupational field. Go ahead and snicker, I deserve it. I'm like King Midas in some alternate universe where things I don't touch turn to gold.</p>

<p>So it is with some trepidation that I once again cast my befouling gaze on my fond old friend C++. I can change, things will be different this time! I won't pine over the preprocessor or pointer arithmetic. Honest!</p>

<p>I think I have a lot of catching up to do.</p>

<ol>
    <li>First, I have to readjust my perspective on coding to include manual memory management, macros, virtual destructors, and avoiding non-const references to temporary objects.</li>
    <li>Next, I need to come up to speed on the <a href="http://www.sgi.com/tech/stl/">standard template library (STL)</a> and <a href="http://en.wikibooks.org/wiki/C++_Programming/RAII">resource acquisition as initialization (RAII)</a>.</li>
    <li>Not sure how much I need to worry about <a href="http://csclub.uwaterloo.ca/media/C++0x%20-%20An%20Overview.html">C++0x</a>.</li>
</ol>

<p>Quite a lot of work ahead. At least I get to use GNU make again. Seriously. No snickering or eyerolling allowed!</p>

<p>This post is just my attempt to motivate myself. Following posts will (hopefully happen and) be substantial.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2008/12/resolving-to-use-cpp-in-2009/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
