<?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>Monzool's Personal Publishing &#187; Programming</title>
	<atom:link href="http://monzool.net/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://monzool.net/blog</link>
	<description>a/ Jan Skriver Sørensen</description>
	<lastBuildDate>Wed, 08 Jun 2011 18:54:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Interruption At Work Created A Mishap</title>
		<link>http://monzool.net/blog/2009/08/30/interruption-at-work-created-a-mishap/</link>
		<comments>http://monzool.net/blog/2009/08/30/interruption-at-work-created-a-mishap/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 10:49:37 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Entertainment]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/?p=101</guid>
		<description><![CDATA[INTERRUPTIONS AT WORK is a frequent occurrence but generally its not a big problem. This time however the unavoidable loss of focus on what you was doing before, gave an unpleasant surprise. I was adding some new functionality and had just written the following: switch &#40;state&#41; &#123; case Step3: configuration.length = 10; break; &#125; Next [...]]]></description>
			<content:encoded><![CDATA[<p><strong>INTERRUPTIONS AT WORK</strong> is a frequent occurrence but generally its not a big problem. This time however the unavoidable loss of focus on what you was doing before, gave an unpleasant surprise.</p>
<p>I was adding some new functionality and had just written the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span>state<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">case</span> Step3<span style="color: #008080;">:</span>
        configuration.<span style="color: #007788;">length</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Next I added a line to specify the configuration data on index zero. With the intention of doing this for the remaining nine data indexes, I copy-pasted the first line and incremented the index.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span>state<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">case</span> ConfigureTask<span style="color: #008080;">:</span>
        configuration.<span style="color: #007788;">data</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> 
        configuration.<span style="color: #007788;">data</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> 
        configuration.<span style="color: #007788;">length</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><p>
But this was at the exact moment a colleague asked a question. To figure out the answer I had to browse around in the same file I was just editing. Not finding the complete answer there, the hunt led on to opening a bunch of other files. Eventually the situation evolved to a discussion using a white-board.
</p>
</p>
<p>Now, even though the above code is incomplete, it compiles to perfectly valid code!. What the above code does is to initialize <code>configuration.data[0]</code>, <code>configuration.data[1]</code> and <code>configuration.length</code> to 10. Naturally this behavior was never the desired behavior for that code&#8230;</p>
<p>Later, returning to my workstation, I had completely forgotten about the unfinished implementation I worked on before. In my mind it was already done and I proceeded on other things that would eventually allow me to run some basic tests for the new implementations. The nature of the code is to delegate a state dependent number of black-box data to a task. The receiving task is found by peeking into the first byte of the black-box data (<code>configuration.data[0]</code>). Unfortunately &#8217;10&#8242; is a perfect match for the first task to be configured. So when unit-testing, at first everything seemed to be okay. </p>
<p>Later some strange behavior appeared, for which I could find no good reasons. Eventually I found the faulting situation in great dismay.</p>
<p>This kind of logic errors is the kind that can become extremely difficult to find, and I&#8217;ve learned my lesson: if leaving in the middle of writing some source code, be sure to quickly add some non-code that will not compile.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2009/08/30/interruption-at-work-created-a-mishap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upgraded From WordPress 2.2.2 to WordPress 2.7</title>
		<link>http://monzool.net/blog/2009/02/08/upgraded-from-wordpress-222-to-wordpress-27/</link>
		<comments>http://monzool.net/blog/2009/02/08/upgraded-from-wordpress-222-to-wordpress-27/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 10:30:34 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/?p=72</guid>
		<description><![CDATA[WORDPRESS 2.7 UPGRADE from WordPress 2.2.2 done with no problems. I was somewhat worried that my old theme would break on the upgrade, but It appears to be working. Well, I got at little issue with the Wp-Syntax plugin. I upgraded the plugin and now my GeShi style overrides do not work anymore &#8211; guess [...]]]></description>
			<content:encoded><![CDATA[<p><strong>WORDPRESS 2.7 UPGRADE</strong> from WordPress 2.2.2 done with no problems.<br />
I was somewhat worried that my old theme would break on the upgrade, but It appears to be working. Well, I got at little issue with the Wp-Syntax plugin. I upgraded the plugin and now my GeShi style overrides do not work anymore &#8211; guess that is things one discover when doing infrequent upgrading.</p>
<p>I&#8217;m looking forward to working with this new WordPress version and exploring its added features. </p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2009/02/08/upgraded-from-wordpress-222-to-wordpress-27/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Numbers To Strings And Back Again &#8211; Standard C++ vs. Boost</title>
		<link>http://monzool.net/blog/2008/05/06/numbers-to-string-and-back-again-standard-c-vs-boost/</link>
		<comments>http://monzool.net/blog/2008/05/06/numbers-to-string-and-back-again-standard-c-vs-boost/#comments</comments>
		<pubDate>Tue, 06 May 2008 21:59:01 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2008/05/06/numbers-to-string-and-back-again-standard-c-vs-boost/</guid>
		<description><![CDATA[CONVERTING NUMBERS TO strings or the opposite of converting strings to numbers, is an operation that is far from as trivial as one would expect from such an obvious task &#8211; at least when it comes to C++ programming using standard libraries. The converting can be performed by the iostringstream classes in the standard library. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>CONVERTING NUMBERS TO</strong> strings or the opposite of converting strings to numbers, is an operation that is far from as trivial as one would expect from such an obvious task &#8211; at least when it comes to C++ programming using standard libraries. The converting can be performed by the <code>iostringstream</code> classes in the standard library. When searching Google for the C++ way of converting between numbers and streams, the <code>stringstream</code> library classes appears not to be the that well known, and especially its features of the number and string operations seems generally to be unknown by many.</p>
<p>The <code>stringstream</code> offers a large range of manipulating stream data, although if used for e.g. special formatted textual output, the implementation steps tends to be somewhat more cumbersome than the old printf family.</p>
<p>The example below takes a few more lines that doing e.g. a <code>atoi</code> or <code>snprintf</code> kind of operation, but depending on the situation, simple conversion scenarios do not require many lines of code.</p>
<h4>Standard Input / Output Streams Library</h4>
<p>The main function is extracted here, just not to obfuscate the picture of the actual converting. Note that <code>stringstream</code> is defined in the <code>&lt;sstream&gt;</code> header.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;sstream&gt;  // stringstream</span>
&nbsp;
<span style="color: #666666;">// Prototypes</span>
<span style="color: #0000ff;">void</span> Std_StringToInteger<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> Std_IntegerToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Std_StringToInteger:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  Std_StringToInteger<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Std_IntegerToString:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  Std_IntegerToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The function below handles conversion from strings to integers. First a simple conversion is done, then followed by an example of testing whether the conversion operation was a success. Last is shown how to enable exceptions on conversion errors.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Std_StringToInteger<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> str <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;1976&quot;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> val<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Load stringstream with text to convert</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">istringstream</span> is<span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// Convert by streaming to integer</span>
  is <span style="color: #000080;">&gt;&gt;</span> val<span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Val: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> val <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Clear stream for another input</span>
  is.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Load stream with a non numeric convertible data</span>
  is.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Monzool.net&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  is <span style="color: #000080;">&gt;&gt;</span> val<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Test if conversion failed</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>is.<span style="color: #007788;">fail</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Conversion failed!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Enable exceptions on conversion errors</span>
  <span style="color: #0000ff;">try</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// Set failures to be thrown as exceptions</span>
    is.<span style="color: #007788;">exceptions</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">istringstream</span><span style="color: #008080;">::</span><span style="color: #007788;">eofbit</span>  <span style="color: #000040;">|</span>
                  std<span style="color: #008080;">::</span><span style="color: #007788;">istringstream</span><span style="color: #008080;">::</span><span style="color: #007788;">failbit</span> <span style="color: #000040;">|</span>
                  std<span style="color: #008080;">::</span><span style="color: #007788;">istringstream</span><span style="color: #008080;">::</span><span style="color: #007788;">badbit</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">catch</span><span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">istringstream</span><span style="color: #008080;">::</span><span style="color: #007788;">failure</span><span style="color: #000040;">&amp;</span> e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Exception: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> e.<span style="color: #007788;">what</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Conversion failed!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>As the naming <code>stringstream</code> indicates, input and output is done by streaming measures. If not quite confident on stream directions, think of how functions <code>cout</code> and <code>cin</code> is used. Using <code>stringstream</code> is no different.</p>
<p>Last function is for converting from numbers to strings.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Std_IntegerToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> val <span style="color: #000080;">=</span> <span style="color: #0000dd;">1976</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Create empty stringstream for number to convert</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">ostringstream</span> os<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// Convert by streaming integer</span>
  os <span style="color: #000080;">&lt;&lt;</span> val<span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Str: &quot;</span> <span style="color: #000080;">&lt;&lt;</span>  os.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h4>Boost lexical_cast</h4>
<p>To put it simple: when dealing with libraries for converting between numbers and strings the Boost library <strong>smokes</strong> the standard C++ library ditto. </p>
<p>
The conversion features of Boost is located in the <a href="http://www.boost.org/doc/libs/1_35_0/libs/conversion/lexical_cast.htm"><code>lexical_cast</code></a> library and is embedded by including the <code>lexical_cast.hpp</code> file (most Boost libraries are implemented in header files and can be embedded by including the appropriate hpp file.).
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;boost/lexical_cast.hpp&gt;</span>
&nbsp;
<span style="color: #666666;">// Prototypes</span>
<span style="color: #0000ff;">void</span> Boost_StringToInteger<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> Boost_IntegerToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Boost_StringToInteger:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  Boost_StringToInteger<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Boost_IntegerToString:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  Boost_IntegerToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Instead of using streaming functionality, Boost has chosen a much more obvious concept. Boost has added the functionality of simply casting between numbers and strings. Casting functions are already a familiar concept in C++, like casting between data types using <code>static_cast</code> or manipulating const&#8217;ness with <code>const_cast</code>.</p>
<p>The <code>lexical_cast</code> template function makes converting from string to integer trivial. The example below also shows how to handle conversion errors by exception handling.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Boost_StringToInteger<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> str <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;1976&quot;</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// Cast string to integer</span>
  <span style="color: #0000ff;">int</span> val <span style="color: #000080;">=</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">lexical_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Val: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> val <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Load string with non numeric convertible data</span>
  str <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;Monzool.net&quot;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">try</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// Non convertible values throws exceptions</span>
    val <span style="color: #000080;">=</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">lexical_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">catch</span> <span style="color: #008000;">&#40;</span>boost<span style="color: #008080;">::</span><span style="color: #007788;">bad_lexical_cast</span> <span style="color: #000040;">&amp;</span>e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Exception: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> e.<span style="color: #007788;">what</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Conversion failed!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Converting the other way from integer to string is just as trivial.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Boost_IntegerToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> val <span style="color: #000080;">=</span> <span style="color: #0000dd;">1976</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// Cast integer to string</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> str <span style="color: #000080;">=</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">lexical_cast</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>val<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  Str: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> str <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p class="section">
When it comes to simple conversion between numbers and strings, Boost is far superior in simplicity. However note that  the design goals have also been very different for the two libraries. The C++ Standard Input/Output Streams Library has been designed for flexibility. And flexible it is indeed, but sadly this side effects to complicating its usage even for obvious tasks that ought to be trivial to perform.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2008/05/06/numbers-to-string-and-back-again-standard-c-vs-boost/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Lambda?</title>
		<link>http://monzool.net/blog/2008/03/19/why-lambda/</link>
		<comments>http://monzool.net/blog/2008/03/19/why-lambda/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 09:58:44 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2008/03/19/why-lambda/</guid>
		<description><![CDATA[I HAVE BEEN reading up on Python programming lately (more on that in a later post). I&#8217;ve now been introduced to anonymous functions. In Python, anonymous functions are available using the lambda keyword. Anonymous functions are great, but I think the Lua syntax for anonymous functions is superior to the syntax adopted in Python. A [...]]]></description>
			<content:encoded><![CDATA[<p><strong>I HAVE BEEN</strong> reading up on Python programming lately (more on that in a <a href="http://monzool.net/blog/2008/03/21/core-python-programming/" class="locallink">later post</a>). I&#8217;ve now been introduced to anonymous functions. In Python, anonymous functions are available using the <code>lambda</code> keyword. Anonymous functions are great, but I think the Lua syntax for anonymous functions is superior to the syntax adopted in Python. </p>
<pre></pre>
<p>A normal function, in Python, is defined using the <code>def</code> keyword along with a function name.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">def</span> f1<span style="color: black;">&#40;</span>x, y<span style="color: black;">&#41;</span>:
...     <span style="color: #ff7700;font-weight:bold;">return</span> x + y
... 
<span style="color: #66cc66;">&gt;&gt;&gt;</span> f1<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">3</span></pre></div></div>

<p>In Python anonymous functions are created by a lambda expression.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> f2 = <span style="color: #ff7700;font-weight:bold;">lambda</span> x, y: x + y
<span style="color: #66cc66;">&gt;&gt;&gt;</span> f2<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">3</span></pre></div></div>

<p>Similar to anonymous function, normal Python functions are first class objects and can be assigned to other variables.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> f = f1
<span style="color: #66cc66;">&gt;&gt;&gt;</span> f<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">3</span></pre></div></div>

<p>However direct assignment of a function deceleration is not possible.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> f = <span style="color: #ff7700;font-weight:bold;">def</span> f3<span style="color: black;">&#40;</span>x, y<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;&lt;stdin&gt;&quot;</span>, line <span style="color: #ff4500;">1</span>
    f = <span style="color: #ff7700;font-weight:bold;">def</span> f3<span style="color: black;">&#40;</span>x, y<span style="color: black;">&#41;</span>:
           ^
<span style="color: #008000;">SyntaxError</span>: invalid syntax</pre></div></div>

<p>This last example resembles the concept of the anonymous function syntax chosen in Lua. First a look on how a normal function is defined in Lua. Its not that different from the Python version.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #b1b100;">function</span> f1<span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;</span>   <span style="color: #b1b100;">return</span> x + y
<span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #b1b100;">end</span>
<span style="color: #66cc66;">&gt;</span> <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span> f1<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">3</span></pre></div></div>

<p>Like in Python, functions are first class objects in Lua and thus also supports aliasing functions.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> f <span style="color: #66cc66;">=</span> f1
<span style="color: #66cc66;">&gt;</span> <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span> f<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">3</span></pre></div></div>

<p>The syntax for anonymous function in Lua differs not much for how normal functions are defined. The function name is omitted (hence anonymous) and secondly the function definition is wrapped in parentheses.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> f2 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;</span>   <span style="color: #b1b100;">return</span> x + y 
<span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #b1b100;">end</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&gt;</span> <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span> f2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">3</span>
<span style="color: #66cc66;">&gt;</span> <span style="color: #808080; font-style: italic;">-- Or as one-liner if preferred</span>
<span style="color: #66cc66;">&gt;</span> f2 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>x, y<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> x + y <span style="color: #b1b100;">end</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&gt;</span> <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span> f2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">3</span></pre></div></div>

<p>In Lua a function is a function and defined as such &#8211; being anonymous or not. I think this approach is more elegant that using a dedicated <code>lambda</code> keyword.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2008/03/19/why-lambda/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What GUI Toolkit To Use?</title>
		<link>http://monzool.net/blog/2008/02/29/what-gui-toolkit-to-use/</link>
		<comments>http://monzool.net/blog/2008/02/29/what-gui-toolkit-to-use/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 13:49:30 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2008/02/29/what-gui-toolkit-to-use/</guid>
		<description><![CDATA[GUI PROGRAMMING IS not something I&#8217;ve done in quite a while. At work I do embedded programming and that&#8217;s mainly also what I&#8217;ve been doing for my own personal projects. Except for some small utility applications I really haven&#8217;t done large GUI projects since MFC 6.0 was cool (if such a time ever was) ]:-&#62;. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>GUI PROGRAMMING IS</strong> not something I&#8217;ve done in quite a while. At work I do embedded programming and that&#8217;s mainly also what I&#8217;ve been doing for my own personal projects. Except for some small utility applications I really haven&#8217;t done large GUI projects since <a href="http://en.wikipedia.org/wiki/Microsoft_Foundation_Class_Library">MFC 6.0</a> was cool (if such a time ever was) <code>]:-&gt;</code>.</p>
<p class="section">
An absolute requirement is that the end result must be multi platform capable (Linux, BSD, Mac OS X and Windows). Plenty of frameworks and toolkits exists that fulfill  that requirement, but I find that the Kde/Qt constallation is the most exiting and complete toolkit(s) around &#8211; especially given the multi platform perspective introduced by Kde 4. Although Kde 4 is not quite stable yet, I think the choice is wise in a longterm perspective.
</p>
<p>
I primarily do C/C++ programming (and a bit of Lua scripting), but I really would like to extend my horizon (or more precise raise above n00b level) in other programming languages like C# and Python. Given that I have much to learn about Kde, GUI&#8217;s and what else is hot in the desktop programming world, the option of C# is not a mandatory requirement. I could settle on a C++ and Python solution.
</p>
<p class="section">
Mixing two complete different kinds of languages (static and dynamic) requires either good binding layers or Mono. The <a href="http://www.kde.org">Kde Project</a> provides a large suite of binders in the KdeBindings package. The <a href="svn://anonsvn.kde.org/home/kde/trunk/KDE/kdebindings/README"> README</a> contains a concise description of the project contents:
</p>
<p>
<em>
<pre>
This package contains:
* working:
  * korundum: KDE bindings for ruby
  * qtruby: Qt bindings for Ruby
  * smoke: Language independent library for Qt and KDE bindings. Used by QtRuby
    and PerlQt.
  * kalyptus: a header parser and bindings generator for Qt/KDE. Used for
    Smoke, Java, C# and KSVG bindings generation at present.
  * ruby/krossruby and python/krosspython which are plugins for the kdelibs/kross
    scripting framework to provide scripting with python+ruby to applications.
  * PyKDE: KDE bindings for python, requires PyQt from riverbankcomputing.co.uk
  * Qyoto: Qt bindings for C#
  * Kimono: KDE bindings for C#
</pre>
<p></em>
</p>
<p>
The Mono project seems to be somewhat controversial. A <a href="http://osnews.com/story/19388/Another_GNOME-Mono_Discussion">lot of writing</a> has being going on lately on Mono vs. Novell/Microsoft vs. freedom. <a href="http://channel9.msdn.com/ShowPost.aspx?PostID=159952">Anders Hejlsberg</a> and his team have created both clever and interesting stuff in the .NET architecture like C#, DTS (Common Type System) and CLR (Common Language Specification), but I can&#8217;t appreciate embracing other closed proprietary technologies from the .NET portfolio, when other alternatives exist in the FOSS community. I think Robert Devi summed it up nice in the <a href="http://osnews.com/permalink?302420">Osnews.com comments</a> (the personal rantings of Robert on Mono speed/memory, Amarok etc. I don&#8217;t agree on).
</p>
<p class="section">
As far as I can tell, the above observations give me the following constructs: </p>
<ol>
<li>C++ + <a href="http://kross.dipe.org">Kross</a> + <a href="http://www.riverbankcomputing.co.uk/pyqt">PyQt</a> + PyKde(<code>*1</code>).</li>
<li><a href="http://www.mono-project.com">Mono</a> + C# + limited managed C++ + <a href="http://cougarpc.net/qyoto">Qyoto/Kimono</a> + <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython">IronPython</a>.</li>
</ol>
<p>
<code>*1</code>: PyKde is not released for Kde 4 at present time.
</p>
<p class="section">
Its a difficult decision whether to choose the one or the other construct. Because I have already done much C++ coding (and <a href="http://www.softpanorama.org/Lang/Cpp_rama/humor.shtml">shot a foot off more that once</a>) and not much C# coding, I lean mostly towards the Mono solution. Unfortunately this could potentially force me to use <a href="http://www.monodevelop.com">MonoDevelop</a>. Tried it eight months ago and tried it again yesterday; it&#8217;s still the single most unstable piece of software I <strong>ever</strong> used <code>:-(</code>. Hope its not the case that it only works on OpenSuse or Suse. That would not be freedom. Anyways, selecting C# would mean that the money I spent on the book <a href="http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764557599.html">Professional C#, 3rd Edition</a> won&#8217;t go to waste.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2008/02/29/what-gui-toolkit-to-use/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Incomprehensive Hexdump Man Page</title>
		<link>http://monzool.net/blog/2008/02/18/incomprehensive-hexdump-man-page/</link>
		<comments>http://monzool.net/blog/2008/02/18/incomprehensive-hexdump-man-page/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 18:41:20 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2008/02/18/incomprehensive-hexdump-man-page/</guid>
		<description><![CDATA[THE HEXDUMP MAN page, I find, is not the best written example of an applications manual. I recently had a task of finding the addresses of filename encounters generated when a bunch of files were written to an uncompressed jffs2 partition. Normally I&#8217;ve been sticking to the simple hexdump -C &#60;device&#62; use, but grepping filenames [...]]]></description>
			<content:encoded><![CDATA[<p><strong>THE HEXDUMP MAN</strong> page, I find, is not the best written example of an applications manual. I recently had a task of finding the addresses of filename encounters generated when a bunch of files were written to an uncompressed jffs2 partition. Normally I&#8217;ve been sticking to the simple <code>hexdump -C &lt;device&gt;</code> use, but grepping filenames from the output is not applyible because of the line breakings.</p>
<pre>
$ hexdump -C /dev/mtd0  | grep count
00092df0  63 6f 75 6e 74 31 32 2e  64 61 74 ff 19 85 e0 02  |count12.dat.....|
000bfe80  00 00 0e 0b 08 63 6f 75  6e 74 31 33 2e 64 61 74  |.....count13.dat|
000c7f10  63 6f 75 6e 74 30 39 2e  64 61 74 ff 19 85 e0 02  |count09.dat.....|
000f9a80  ff 40 62 1d f9 72 7e e3  63 6f 75 6e 74 31 31 2e  |.@b..r~.count11.|
000ffb90  63 6f 75 6e 74 30 39 2e  64 61 74 e0 02 00 00 00  |count09.dat.....|
000ffd80  0a 00 00 00 0b 0b 08 63  6f 75 6e 74 31 30 2e 64  |.......count10.d|
00115e20  bd 6e 58 e6 63 6f 75 6e  74 30 37 2e 64 61 74 ff  |.nX.count07.dat.|
0012ebe0  63 6f 75 6e 74 30 38 2e  64 61 74 ff 19 85 e0 02  |count08.dat.....|
0013fcc0  00 08 0b 08 63 6f 75 6e  74 30 37 2e 64 61 74 e0  |....count07.dat.|
0013feb0  01 00 00 00 08 00 00 00  09 0b 08 63 6f 75 6e 74  |...........count|
0014af40  fa ce 22 36 63 6f 75 6e  74 30 34 2e 64 61 74 ff  |.."6count04.dat.|
00163d00  63 6f 75 6e 74 30 35 2e  64 61 74 ff 19 85 e0 02  |count05.dat.....|
0017fbc0  00 00 00 05 0b 08 63 6f  75 6e 74 30 34 2e 64 61  |......count04.da|
0017ffb0  00 07 0b 08 63 6f 75 6e  74 30 36 2e 64 61 74 e0  |....count06.dat.|
00180070  16 b6 2c e3 32 2e ad 46  63 6f 75 6e 74 30 31 2e  |..,.2..Fcount01.|
00198e30  75 8e d7 96 63 6f 75 6e  74 30 32 2e 64 61 74 ff  |u...count02.dat.|
001b1bf0  63 6f 75 6e 74 30 33 2e  64 61 74 ff 19 85 e0 02  |count03.dat.....|
001bfb00  0b 08 63 6f 75 6e 74 30  31 2e 64 61 74 e0 02 00  |..count01.dat...|
001bfcf0  00 00 02 00 00 00 03 0b  08 63 6f 75 6e 74 30 32  |.........count02|
001bfef0  63 6f 75 6e 74 30 33 2e  64 61 74 e0 02 00 00 00  |count03.dat.....|
</pre>
<p>Wanting to hexdump to produce an output more suitable for searching, I read the <a href="http://linux.about.com/library/cmd/blcmdl1_hexdump.htm">hexdump man page</a> where it is evident that hexdump provides flexible output formatting.</p>
<pre>
     -e format_string
             Specify a format string to be used for displaying data.
</pre>
<p>The short description is elaborated in a later section</p>
<pre>
   Formats
     A format string contains any number of format units, separated by white-
     space.  A format unit contains up to three items: an iteration count, a
     byte count, and a format.
</pre>
<p>Okay, three parameters of which two of them are optional. Regarding the non-optional format specifier, it must be double quoted.</p>
<pre>
     The format is required and must be surrounded by double quote (" ")
     marks. It is interpreted as a fprintf-style format string (see
     fprintf(3)) ...
</pre>
<p>Okay. Not so hard. I know fprintf syntax. So what configuration am I optionally skipping? The first parameter is iteration count.</p>
<pre>
     The iteration count is an optional positive integer, which defaults to
     one.  Each format is applied iteration count times.
</pre>
<p>
So, what does the iteration count actually do? Repeat the same printout x number of times? That of course would be a daft thing to do. Not being a native English speaker, I reassured that iteration did not have any dualistic meaning unknown to me. <a href="http://dictionary.reference.com/browse/iteration">Dictionary.com</a> defines</p>
<p><em></p>
<p>it·er·a·tion</p>
<ol>
<li>1.	the act of repeating; a repetition.</li>
<li>2.	Mathematics.<br />
a.	Also called successive approximation. a problem-solving or computational method in which a succession of approximations, each building on the one preceding, is used to achieve a desired degree of accuracy.<br />
b.	an instance of the use of this method.</li>
<li>3.	Computers. a repetition of a statement or statements in a program.</li>
</ol>
<p></em></p>
<p>Hmm, still not exactly clear on what iteration does. I&#8217;d better experiment to figure it out. Next optional parameter defines a byte count.</p>
<pre>
     The byte count is an optional positive integer.  If specified it defines
     the number of bytes to be interpreted by each iteration of the format.
</pre>
<p>Huh? Byte count of what again? Does this relate to the amount of <code>"%c"</code>&#8216;s and what-not I put in the mandatory part?. Experimentations will tell. The final details on the optional parameters are how to apply them.</p>
<pre>
     If an iteration count and/or a byte count is specified, a single slash
     must be placed after the iteration count and/or before the byte count to
     disambiguate them.
</pre>
<p>That would be <code>iterations</code> or <code>iterations/byte_count</code> told in many words forming an obscure sentence?</p>
<p class="section">Well, feeling armed for some basic hexdump formatting, I proceeded to do some experimentations.</p>
<pre>
$ hexdump -e "0x%08x" /dev/mtd0
hexdump: bad format {0x%08x}
</pre>
<p>What<bold>?!</bold> I took another look at the examples provided by the man page.</p>
</p>
<pre>
     Display the input in perusal format:

           "%06.6_ao "  12/1 "%3_u "
           "\\t\\t" "%_p "
           "\\n"
</pre>
<p>Hmm, and I write all three lines how? Or is it three examples? Tried the top line from the example. It worked, although of course not giving me the output format desired. Cos of the nature of the input data, the generated output actually didn&#8217;t make much sense, but now a little wiser I continued experimenting.</p>
</p>
<pre>
$ hexdump -e 8/1 "0x%08x" /dev/mtd0
hexdump: bad format {8/1}
</pre>
<p>Hmm, perhaps a double qouting is required before the &#8220;optional&#8221; parameters?.</p>
<pre>
-sh-2.05b# hexdump -e "" 8/1 "0x%08x" /dev/mtd0
Segmentation fault
</pre>
<p><strong>WTF!?</strong> Near having a fury induced head explosion I resolved to Google. Seems that <a href="http://www.technovelty.org/linux/tips/hexdump.html">I&#8217;m not the only one having a hard time decoding the &#8216;-e&#8217; description</a> (even though the kind poster states that reading the man page is understanding hexdump). The apparent proper syntax is:</p>
<pre>
$ hexdump -e ' [iterations]/[byte_count] "[format string]" '
</pre>
<p>This was not the exact syntax mentioned in the man page, but I tried.</p>
<pre>
$ hexdump -e '6/1 "0x%08x, "' -e '"\\n"' /dev/mtd0
</pre>
<p>Hurraa, it worked. Having this figured out, the only thing left was to find out what the exact functionality of the <code>iteration</code> and <code>byte_count</code> parameters were? I wasn&#8217;t fully enlightened by the output, so a few more tests should reveal the purpose of them both.</p>
<pre>
$ hexdump -e '6/1 "0x%08x, "' -e '"\\n"' /dev/mtd0
0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff,
0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff,
</pre>
<p>Hmm, six columns&#8230;</p>
<pre>
$ hexdump -e '4/1 "0x%08x, "' -e '"\\n"' /dev/mtd0
0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff,
0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff,
0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff,
0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff,
</pre>
<p>Aha, so&#8230; iterations equals <strong>columns</strong>. Still not figured out the byte_count parameter though.</p>
<pre>
$ hexdump -e '6/2 "0x%08x, "' -e '"\\n"' /dev/mtd0
0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff,
</pre>
<p>
Perhaps the partial empty (erased) flash section was not the best example to learn from, so I created a file repeating the numbers 00 to 09.</p>
<pre>
$ hexdump -e '6/1 "0x%08x, "' -e '"\\n"' count.hex
0x00000000, 0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005,
0x00000006, 0x00000007, 0x00000008, 0x00000009, 0x00000000, 0x00000001,
0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006, 0x00000007,
0x00000008, 0x00000009, 0x00000000, 0x00000001, 0x00000002, 0x00000003,
</pre>
<pre>
$ hexdump -e '6/2 "0x%08x, "' -e '"\\n"' count.hex
0x00000100, 0x00000302, 0x00000504, 0x00000706, 0x00000908, 0x00000100,
0x00000302, 0x00000504, 0x00000706, 0x00000908, 0x00000100, 0x00000302,
0x00000504, 0x00000706, 0x00000908, 0x00000100, 0x00000302, 0x00000504,
0x00000706, 0x00000908, 0x00000100, 0x00000302, 0x00000504, 0x00000706,
</pre>
<p>Aha, guess <em>that</em>(?) would fit the byte count description&#8230;</p>
<p>Having finally decoded the man page I set on to find a proper output. After some unsuccessful attempts, I googled for a hint to a solution. Eventually I found some indications that, to get the desired formatting, I should utilize some of the non-fprintf formatting options provided by hexdump. More man page decoding? No fucking way! Enough of this shit!</p>
<p class="section">Having wasted precious work time, I abandoned hexdump and put together a little Lua script that would do the hex dumping and format the output to fit my requirements.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="lua" style="font-family:monospace;">#<span style="color: #66cc66;">!</span>lua
<span style="color: #808080; font-style: italic;">--[[  Hex dump utility
      usage:   lua xdex.lua pattern file
&nbsp;
      example: lua xdex.lua &quot;count%d%d.dat&quot; file.dat
--]]</span>
&nbsp;
<span style="color: #b1b100;">local</span> <span style="color: #b1b100;">debug</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">false</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- http://lua-users.org/wiki/LuaPrintf</span>
printf <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>s,...<span style="color: #66cc66;">&#41;</span>
           <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span> s:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>...<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
         <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
&nbsp;
<span style="color: #b1b100;">local</span> f <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">assert</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">io.open</span><span style="color: #66cc66;">&#40;</span>arg<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">&quot;rb&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">local</span> data <span style="color: #66cc66;">=</span> f:<span style="color: #b1b100;">read</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;*all&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">-- Locate offsets of all pattern matching items</span>
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #b1b100;">local</span> offset_begin, offset_end <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>
<span style="color: #b1b100;">local</span> items <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
<span style="color: #b1b100;">local</span> index <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
&nbsp;
<span style="color: #b1b100;">repeat</span>
  offset_begin, offset_end <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">string.find</span><span style="color: #66cc66;">&#40;</span> data, arg<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, offset_begin+<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">if</span> offset_begin <span style="color: #66cc66;">==</span> <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">then</span>
    <span style="color: #b1b100;">break</span>
  <span style="color: #b1b100;">end</span>
  items<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> offset_begin, offset_end <span style="color: #66cc66;">&#125;</span>
  index <span style="color: #66cc66;">=</span> index+<span style="color: #cc66cc;">1</span>
  <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">debug</span> <span style="color: #b1b100;">then</span> printf<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%08xh - %08xh<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, offset_begin, offset_end<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">end</span>
<span style="color: #b1b100;">until</span> <span style="color: #66cc66;">&#40;</span> offset_begin <span style="color: #66cc66;">==</span> <span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">&#41;</span>
items<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">nil</span>, <span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">-- Terminate</span>
&nbsp;
<span style="color: #808080; font-style: italic;">--</span>
<span style="color: #808080; font-style: italic;">--    Hexdump alike printing of results</span>
<span style="color: #808080; font-style: italic;">--    (Inspired from test/xd.lua in lua5.1 distribution)</span>
index <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
<span style="color: #b1b100;">local</span> offset <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
<span style="color: #b1b100;">while</span> <span style="color: #b1b100;">true</span> <span style="color: #b1b100;">do</span>
  <span style="color: #b1b100;">local</span> s <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">string.sub</span><span style="color: #66cc66;">&#40;</span> data, offset+<span style="color: #cc66cc;">1</span>, offset+<span style="color: #cc66cc;">16</span> <span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">if</span> s <span style="color: #66cc66;">==</span> <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">or</span> items<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">then</span>
    <span style="color: #b1b100;">return</span>
  <span style="color: #b1b100;">end</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>offset+<span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;=</span> items<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">then</span>
    <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span> <span style="color: #b1b100;">string.format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%08x  &quot;</span>, offset<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">string.gsub</span><span style="color: #66cc66;">&#40;</span> s,<span style="color: #ff0000;">&quot;(.)&quot;</span>,
        <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span> <span style="color: #b1b100;">string.format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%02x &quot;</span>,<span style="color: #b1b100;">string.byte</span><span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">end</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span> <span style="color: #b1b100;">string.rep</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span>-<span style="color: #b1b100;">string.len</span><span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">io.write</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot; &quot;</span>, <span style="color: #b1b100;">string.gsub</span><span style="color: #66cc66;">&#40;</span>s,<span style="color: #ff0000;">&quot;%c&quot;</span>,<span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>offset+<span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;=</span> items<span style="color: #66cc66;">&#91;</span>index<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">then</span>
      index <span style="color: #66cc66;">=</span> index+<span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">end</span>
  <span style="color: #b1b100;">end</span>
  offset<span style="color: #66cc66;">=</span>offset+<span style="color: #cc66cc;">16</span>
<span style="color: #b1b100;">end</span></pre></td></tr></table></div>

<p>The output from the above script correctly finds 26 encounters of the input pattern, where the original grepping on the hexdump output would only discover 20 encounters.</p>
<pre>
$ xdex.lua "count%d%d*[.]dat" /dev/mtd0
00092df0  63 6f 75 6e 74 31 32 2e 64 61 74 ff 19 85 e0 02  count12.datÿ.à.
000abba0  0b 08 00 00 03 f0 42 2c 83 b2 2d 83 63 6f 75 6e  .....ðB,²-coun
000abbb0  74 31 33 2e 64 61 74 ff 19 85 e0 02 00 00 10 44  t13.datÿ.à....D
000bfc80  00 00 00 01 00 00 00 0c 00 00 00 0d 0b 08 63 6f  ..............co
000bfc90  75 6e 74 31 32 2e 64 61 74 e0 02 00 00 00 0d 00  unt12.datà......
000bfe80  00 00 0e 0b 08 63 6f 75 6e 74 31 33 2e 64 61 74  .....count13.dat
000c7f10  63 6f 75 6e 74 30 39 2e 64 61 74 ff 19 85 e0 02  count09.datÿ.à.
000e0cc0  0b 08 00 00 61 f7 3b 03 c4 12 57 53 63 6f 75 6e  ....a÷;.Ä.WScoun
000e0cd0  74 31 30 2e 64 61 74 ff 19 85 e0 02 00 00 10 44  t10.datÿ.à....D
000f9a80  ff 40 62 1d f9 72 7e e3 63 6f 75 6e 74 31 31 2e  ÿ@b.ùr~ãcount11.
000f9a90  64 61 74 ff 19 85 e0 02 00 00 10 44 ee 2d 30 6f  datÿ.à....Dî-0o
000ffb90  63 6f 75 6e 74 30 39 2e 64 61 74 e0 02 00 00 00  count09.datà....
000ffd80  0a 00 00 00 0b 0b 08 63 6f 75 6e 74 31 30 2e 64  .......count10.d
000ffd90  61 74 e0 02 00 00 00 0b 00 00 00 02 00 02 0c d8  atà............Ø
000fff70  00 00 00 01 00 00 00 0b 00 00 00 0c 0b 08 63 6f  ..............co
000fff80  75 6e 74 31 31 2e 64 61 74 e0 02 00 00 00 0c 00  unt11.datà......
00115e20  bd 6e 58 e6 63 6f 75 6e 74 30 37 2e 64 61 74 ff  ½nXæcount07.datÿ
0012ebe0  63 6f 75 6e 74 30 38 2e 64 61 74 ff 19 85 e0 02  count08.datÿ.à.
0013fcc0  00 08 0b 08 63 6f 75 6e 74 30 37 2e 64 61 74 e0  ....count07.datà
0013feb0  01 00 00 00 08 00 00 00 09 0b 08 63 6f 75 6e 74  ...........count
0013fec0  30 38 2e 64 61 74 e0 02 00 00 00 09 00 00 00 02  08.datà.........
0014af40  fa ce 22 36 63 6f 75 6e 74 30 34 2e 64 61 74 ff  úÎ"6count04.datÿ
00163d00  63 6f 75 6e 74 30 35 2e 64 61 74 ff 19 85 e0 02  count05.datÿ.à.
0017cab0  0b 08 00 00 b7 fd 21 e2 80 0e 71 56 63 6f 75 6e  ....·ý!â.qVcoun
0017cac0  74 30 36 2e 64 61 74 ff 19 85 e0 02 00 00 10 44  t06.datÿ.à....D
0017fbc0  00 00 00 05 0b 08 63 6f 75 6e 74 30 34 2e 64 61  ......count04.da
0017fbd0  74 e0 02 00 00 00 05 00 00 00 02 00 00 af 50 00  tà...........¯P.
0017fdb0  00 00 01 00 00 00 05 00 00 00 06 0b 08 63 6f 75  .............cou
0017fdc0  6e 74 30 35 2e 64 61 74 e0 02 00 00 00 06 00 00  nt05.datà.......
0017ffb0  00 07 0b 08 63 6f 75 6e 74 30 36 2e 64 61 74 e0  ....count06.datà
00180070  16 b6 2c e3 32 2e ad 46 63 6f 75 6e 74 30 31 2e  .¶,ã2.­Fcount01.
00180080  64 61 74 ff 19 85 e0 02 00 00 10 44 ee 2d 30 6f  datÿ.à....Dî-0o
00198e30  75 8e d7 96 63 6f 75 6e 74 30 32 2e 64 61 74 ff  u×count02.datÿ
001b1bf0  63 6f 75 6e 74 30 33 2e 64 61 74 ff 19 85 e0 02  count03.datÿ.à.
001bfb00  0b 08 63 6f 75 6e 74 30 31 2e 64 61 74 e0 02 00  ..count01.datà..
001bfcf0  00 00 02 00 00 00 03 0b 08 63 6f 75 6e 74 30 32  .........count02
001bfd00  2e 64 61 74 e0 02 00 00 00 03 00 00 00 02 00 01  .datà...........
001bfef0  63 6f 75 6e 74 30 33 2e 64 61 74 e0 02 00 00 00  count03.datà....
</pre>
<p class="section">Thank you Lua, thou truly are a light in the darkness.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2008/02/18/incomprehensive-hexdump-man-page/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>C++ Function Hidden, Not Overloaded Nor Overridden</title>
		<link>http://monzool.net/blog/2008/02/12/c-function-hidden-not-overloaded-nor-overridden/</link>
		<comments>http://monzool.net/blog/2008/02/12/c-function-hidden-not-overloaded-nor-overridden/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 22:12:18 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2008/02/12/c-function-hidden-not-overloaded-nor-overridden/</guid>
		<description><![CDATA[THE C++ INHERITANCE model can be unintuitive some times &#8211; or perhaps more correctly, its easy to get tricked by C++ in some circumstances. When having an existing code base the need sometimes comes up, that an often used class is needed with a few additional features. Not to mess with any of existing code, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>THE C++ INHERITANCE</strong> model can be unintuitive some times &#8211; or perhaps more correctly, its easy to get tricked by C++ in some circumstances.</p>
<p>
When having an existing code base the need sometimes comes up, that an often used class is needed with a few additional features. Not to mess with any of existing code, a new class is created deriving from the original class. No functionality can be extended without breaking any existing code using the original code &#8211; sweet. But care is advised, or one might inadvertently step on a landmine.</p>
<p>The example below is a snippet from a boat class that provides a member for setting the speed of the boat. Positive values indicates forward sailing while negative values is backward sailing. Given the task of producing a super fast race boat a new class <code>FastBoat</code> is derived so that unrealistically high speeds can be executed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Boat
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
          <span style="color: #0000ff;">virtual</span> ~Boat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
          <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Speed<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> speed<span style="color: #008000;">&#41;</span>
          <span style="color: #008000;">&#123;</span>
            std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Speed of boat: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> speed <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
          <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> FastBoat<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Boat
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
          <span style="color: #0000ff;">virtual</span> ~FastBoat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
&nbsp;
          <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Speed<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> speed<span style="color: #008000;">&#41;</span>
          <span style="color: #008000;">&#123;</span>
            std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Speed of fast boat: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> speed <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
          <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Maneuvring the boat<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ForwardKnots  <span style="color: #000080;">=</span> <span style="color: #0000dd;">22</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span>          BackwardKnots <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
&nbsp;
  FastBoat fastBoat<span style="color: #008080;">;</span>
  fastBoat.<span style="color: #007788;">Speed</span><span style="color: #008000;">&#40;</span>ForwardKnots<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  fastBoat.<span style="color: #007788;">Speed</span><span style="color: #008000;">&#40;</span>BackwardKnots<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">EXIT_SUCCESS</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Glancing at the code one might be convinced that all is fine an dandy. Setting unsigned speeds on a <code>FastBoat</code> would trigger the <code>FastBoat</code> object, while signed speeds would propagate to the base <code>Boat</code> object.</p>
<pre></pre>
<p>This, however, is the output produced by the example code:</p>
<pre>
Speed of fast boat: 22
Speed of fast boat: 4294967293
</pre>
<p>Wanting to reverse the boat at mere 3 knots, the boat is sent forward at cartoon-fast speed. So what just happened?. Well, the <code>Boat::Speed</code> function was not called, and instead the <code>BackwardKnots</code> value was casted to fit the <code>FastBoat::Speed</code> function. This is because <em>function overload resolution does not cross inheritance boundaries</em> &#8211; that is, not by default. For the above code to work as intended, the hidden function from the base class must be brought into scope.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> FastBoat<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Boat
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
          <span style="color: #0000ff;">virtual</span> ~FastBoat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span>
          <span style="color: #0000ff;">using</span> Boat<span style="color: #008080;">::</span><span style="color: #007788;">Speed</span><span style="color: #008080;">;</span>    <span style="color: #666666;">// Bring Boat::String function into scope</span>
          <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Speed<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> speed<span style="color: #008000;">&#41;</span>
          <span style="color: #008000;">&#123;</span>
            std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Speed of fast boat: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> speed <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
          <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Daring another attempt to test the program, the result now is as intended.</p>
<pre>
Speed of fast boat: 22
Speed of fast boat: -3
</pre>
<p>The <code>using</code> directive has brought the base class function into the namespace scope and is thus called correctly as it is no longer hidden.</p>
<p>Its an easy mistake to make and thats surely why other languages (e.g. D and C#) have introduced keyword for explicitly specifying what action intended. If using GCC compiling with the <code>-Woverloaded-virtual</code> options is recommended for catching these kind of mistakes.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2008/02/12/c-function-hidden-not-overloaded-nor-overridden/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Help On Python Regular Expressions</title>
		<link>http://monzool.net/blog/2007/10/15/help-on-python-regular-expressions/</link>
		<comments>http://monzool.net/blog/2007/10/15/help-on-python-regular-expressions/#comments</comments>
		<pubDate>Mon, 15 Oct 2007 20:38:21 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2007/10/15/help-on-python-regular-expressions/</guid>
		<description><![CDATA[REGULAR EXPRESSIONS ARE a powerful friend, but the friendship doesn&#8217;t come easy. Regular expressions can be somewhat baffling getting a grasp on, but when finally understood, the possibilities are almost endless. When developing the searching expression used in HTML Parsing With Beautiful Soup I realized that my regular expression knowledge had gotten a bit rusty. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>REGULAR EXPRESSIONS ARE</strong> a powerful friend, but the friendship doesn&#8217;t come easy. <a href="http://www.regular-expressions.info/" title="Regular Expression information">Regular expressions</a> can be somewhat baffling getting a grasp on, but when finally understood, the possibilities are almost endless.</p>
<p>When developing the searching expression used in <a href="http://monzool.net/blog/2007/10/15/html-parsing-with-beautiful-soup/" class="locallink">HTML Parsing With Beautiful Soup</a> I realized that my regular expression knowledge had gotten a bit rusty. Fortunately I had double-up on the luck. 1) It was a Python program, hence the Python shell was available. 2) I found <a href="http://en.wikipedia.org/wiki/David_Mertz">David Mertz</a>&#8216;s book <a href="http://gnosis.cx/TPiP/">Text Processing in Python</a>.
</p>
<p>The Python shell makes it easy to experiment and tweak any regular expressions on the fly, but the downside is that its not easy to visually evaluate the outcome of your current expression. David&#8217;s book helped two folds. It has extensive theory on Python regular expression syntax, but most superhero-like is the small function provided, that makes it possible to see the outcome of an evaluated expression.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Credits: David Mertz</span>
<span style="color: #ff7700;font-weight:bold;">def</span> re_show<span style="color: black;">&#40;</span>pat, s<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span> pat, <span style="color: #dc143c;">re</span>.<span style="color: black;">M</span> <span style="color: black;">&#41;</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;{<span style="color: #000099; font-weight: bold;">\g</span>&lt;0&gt;}&quot;</span>, s.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span></pre></div></div>

<p>
<p class="section">Using regular expressions in Python requires importing of the regular expression libirary.</p>
</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span></pre></div></div>

<p>
<p>If using the Python shell just enter the same in the shell prompt. The function by David Mertz can also be typed directly into the shell</p>
</p>
<div class="dotbox">
<pre>
>>> import re
>>> def re_show(pat, s):
...    print re.compile( pat, re.M ).sub( "{\g<0>}", s.rstrip() ), '\n'
>>>
</pre>
</div>
<p>
<p>The <code>re_show</code> wrapper displays the source and emphasizes the result of the expression, as being the contents between the &#8216;{&#8216; and &#8216;}&#8217; pair.
</p>
<p>
<p class="section">Next is creation of some example text on which to experiment.</p>
</p>
<div class="dotbox">
<pre>
>>> s = 'if (Hulk.color != "green"): print "Grey Hulk"'
</pre>
</div>
<p>
<p class="section">Now the experiments can begin. The following searches for everything between the first &#8216;(&#8216; to the last &#8216;)&#8217;.</p>
</p>
<div class="dotbox">
<pre>
>>> re_show(r'\(.*\)', s)
</pre>
</div>
<p>Result: </p>
<p>
  <code>if</code><strong><code>{</code></strong><code>(Hulk.color != "green")</code><strong><code>}</code></strong><code>: print "Grey Hulk"</code>
  </p>
</p>
<p class="section">
<p>Another example could be an case-insensitive match on the colors of Hulk.</p>
<div class="dotbox">
<pre>
>>> re_show(r'(?i)green|(?i)grey', s)
</pre>
</div>
<div></div>
<p>Result: </p>
<p>
  <code>if (Hulk.color != "</code><strong><code>{</code></strong><code>green</code><strong><code>}</code></strong><code>"): print "</code><strong><code>{</code></strong><code>Grey</code><strong><code>}</code></strong><code>Hulk"</code>
 </p>
</p>
<p class="section">This is just at minuscule introduction to the powers of regular expressions. If your into regular expressions in Python, I highly recommend to buying the book &#8211; or donate and read it online.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2007/10/15/help-on-python-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HTML Parsing With Beautiful Soup</title>
		<link>http://monzool.net/blog/2007/10/15/html-parsing-with-beautiful-soup/</link>
		<comments>http://monzool.net/blog/2007/10/15/html-parsing-with-beautiful-soup/#comments</comments>
		<pubDate>Mon, 15 Oct 2007 18:32:00 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2007/10/15/html-parsing-with-beautiful-soup/</guid>
		<description><![CDATA[BEAUTIFUL SOUP IS an HTML/XML parser written in Python. Beautiful Soup excels as an easy to use parser that requires no knowledge of actual parsing theory and techniques. And thanks to the excellent documentation with many code examples, it is easy to fabricate some working code very quickly. On Debian, Beautiful Soup can be install [...]]]></description>
			<content:encoded><![CDATA[<p><strong>BEAUTIFUL SOUP IS</strong> an HTML/XML parser written in Python. <a href="http://www.crummy.com/software/BeautifulSoup/" title="Beautiful Soup">Beautiful Soup</a> excels as an easy to use parser that requires no knowledge of actual parsing theory and techniques. And thanks to the <a href="http://www.crummy.com/software/BeautifulSoup/documentation.html" title="Beautiful Soup documentation">excellent documentation</a> with many code examples, it is easy to fabricate some working code very quickly.</p>
<p class="section">On Debian, Beautiful Soup can be install via <em>apt-get</em> / <em>aptitude</em>:<br />
<code>aptitude install python-beautifulsoup</code></p>
<p class="section">The example below extracts the hit counter from this very page. Note that this is perhaps not the best example in the world (the only parse value used is the &#8220;footer&#8221; section), but it does exemplifies how easily the process of extracting data from a HTML page can be done when utilizing the Beautiful Soup parser.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># coding=utf-8</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> BeautifulSoup <span style="color: #ff7700;font-weight:bold;">import</span> BeautifulSoup          <span style="color: #808080; font-style: italic;"># For processing HTML</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>                                   <span style="color: #808080; font-style: italic;"># URL tools</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>                                        <span style="color: #808080; font-style: italic;"># Regular expressions</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> FindHits<span style="color: black;">&#40;</span>proxyUrl<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># URL to HTML parse</span>
    url = <span style="color: #483d8b;">'http://monzool.net/blog/index.php'</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>proxyUrl<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
        <span style="color: #808080; font-style: italic;"># Proxy set up</span>
        proxy = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">ProxyHandler</span><span style="color: black;">&#40;</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'http'</span>: proxyUrl<span style="color: black;">&#125;</span> <span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Create an URL opener utilizing proxy</span>
        opener = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span> proxy <span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">install_opener</span><span style="color: black;">&#40;</span> opener <span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Aquire data from URL</span>
        request = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">Request</span><span style="color: black;">&#40;</span> url <span style="color: black;">&#41;</span>
        response = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span> request <span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #808080; font-style: italic;"># Aquire data from URL</span>
        response = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span> url <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Extract data as HTML data</span>
    html = response.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Parse HTML data</span>
    soup = BeautifulSoup<span style="color: black;">&#40;</span> html <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Search requested page for &lt;div&gt; section with id=&quot;footer&quot;</span>
    <span style="color: #808080; font-style: italic;"># (The result is returned in unicode)</span>
    footer = soup.<span style="color: black;">findAll</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">'div'</span>, <span style="color: #008000;">id</span>=<span style="color: #483d8b;">&quot;footer&quot;</span> <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Hint: on this site, it is known that only a single &quot;footer&quot; section</span>
    <span style="color: #808080; font-style: italic;"># exists, and that the hit counter resides in that same section</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Search for the frase &quot;Hits=&lt;some number&gt;&quot;</span>
    pattern = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span> r<span style="color: #483d8b;">'Hits=.*[0-9]'</span> <span style="color: black;">&#41;</span>
    items = <span style="color: #dc143c;">re</span>.<span style="color: black;">findall</span><span style="color: black;">&#40;</span> pattern, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>footer<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Print result</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> items<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>        <span style="color: #808080; font-style: italic;"># -&gt; &quot;Hits=&lt;count&gt;&quot;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Processing...&quot;</span>
    FindHits<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>          <span style="color: #808080; font-style: italic;"># Supply proxy if required. </span>
                          <span style="color: #808080; font-style: italic;"># FindHist(&quot;http://&lt;proxyname&gt;:&lt;port&gt;&quot;)</span></pre></td></tr></table></div>

<p><p class="section">Explanation: If connecting to the internet through a proxy, some additional setup must be done to <a href="http://www.voidspace.org.uk/python/articles/urllib2.shtml" title="urlib2 tutorial">urllib2</a>. Although urllib2 do provide some automatic proxy configuration detection, but here the configuration is made explicitly.</p>
<p>When the URL is opened the HTML is feed to the Beautiful Soup parser. Here after the member call <code>findAll</code> is used for finding the HTML div section identified as &#8220;footer&#8221; (<code>&lt;div id="footer"&gt;</code>). As noted, no further parsing is done, as this page on contains only one footer section, but otherwise Beautiful Soup provides functions like <code>findAllNext</code> and <code>findNextSiblings</code> to iterate through the parse tree (Beautiful Soup is unicode aware, but not using it in this example, so converting the found section to ascii before inputting it to <code>findall</code>).</p>
<p>The resulting output from the search is the hit counter is extracted from this page.</p></p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2007/10/15/html-parsing-with-beautiful-soup/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ Contructor/Destructor Call Order</title>
		<link>http://monzool.net/blog/2007/09/19/c-contructordestructor-call-order/</link>
		<comments>http://monzool.net/blog/2007/09/19/c-contructordestructor-call-order/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 19:18:18 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2007/09/19/c-contructordestructor-call-order/</guid>
		<description><![CDATA[WHEN BEGINNING C++ programming I&#8217;ve experienced people to have trouble remembering the correct construction and destruction call order. Personally I conquered this using a common memory technique &#8211; creating a story. Here are two stories that might help in remembering. The stories might seem quite long, but don&#8217;t fear, it&#8217;s not necessary to remember the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>WHEN BEGINNING C++</strong> programming I&#8217;ve experienced people to have trouble remembering the correct construction and destruction call order. Personally I conquered this using a common memory technique &#8211; creating a story.</p>
<p>Here are two stories that might help in remembering. The stories might seem quite long, but don&#8217;t fear, it&#8217;s not necessary to remember the stories word by word. Btw. the storytelling does not fly 100[%], but they are good enough&#8230; at least for me ;-)</p>
<h4>Story #1: The Skyscraper</h4>
<p>The skyscraper story is exemplified from this simple class structure:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Base
<span style="color: #008000;">&#123;</span>
  Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Base&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Derived <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Base
<span style="color: #008000;">&#123;</span>
  Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Derived&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>A couple of prerequisites are necessary:</p>
<p>1) Imagine the UML class diagram of the above turned upside down.</p>
<p><img src="http://monzool.net/blog/wp-content/uploads/2007/09/class_diagram_buttom_up.jpg" class="disp_center" alt="Class diagram turned upside down" /></p>
<p>2) Imagine that the each class in the upside down diagram represents a floor in a skyscraper.</p>
<p><img src="http://monzool.net/blog/wp-content/uploads/2007/09/class_diagram_to_building.jpg" class="disp_center" alt="Class diagram is a building" /></p>
<p>The <code>Base</code> is the <em>foundation</em> and all <code>Derived</code> objects are <em>floors</em> that builds upon that foundation.</p>
<p>
<img src="http://monzool.net/blog/wp-content/uploads/2007/09/class_building.jpg" class="disp_center" alt="Construction" /><br />
<code>Start constructing here ---&gt;</code></p>
<p>So to construct a skyscraper the building process must be <code>Base</code> first, then <code>Derived</code> next, as buildings (<a href="http://en.wikipedia.org/wiki/BMW_Headquarters" title="BMW Headquarter">usually</a>) are build from bottom and upwards.</p>
<p>When destructing the destruction order is the same as dismantling a building, top to bottom.</p>
<p><code>Start destructing here ---&gt;</code><br />
<img src="http://monzool.net/blog/wp-content/uploads/2007/09/class_building.jpg" class="disp_center" alt="Destruction" /></p>
<p class="section">So what about the special case of destruction when the object is polymorphic?</p>
<p>When allocated is looks like its a building with only a base level; even though we know its a building two storages high.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>10
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">Base<span style="color: #000040;">*</span> pBase <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Derived<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>The problem here is that the demolition team only have access to the base level and when destroying the building, disaster will happen&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">delete</span> pBase<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>As the building <strong>is</strong> two storages high, it will collapse when the supporting foundation is removed first (allegorizing a bad situation as the <code>Derived</code> object would not get destroyed when only deleting the base object).</p>
<p>So how to fix this situation? You provide the demolition team with an <em>elevator</em>. The &#8220;elevator&#8221; is a special demolition model called <code>virtual</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
15
16
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Base
<span style="color: #008000;">&#123;</span>
  Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Base&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">virtual</span> ~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;~Base&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>  <span style="color: #666666;">// Virtual destructor</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>As the building is equipped with an elevator the demolition team can escalate to the top of the building and begin the destruction from top to bottom and get everything removed properly.</p>
<h4>Story #2: File Manipulation</h4>
<p>The second story relate the base/derived situation to file contents manipulation.</p>
<p>A file must be opened before it can be closed, and if opened it must be closed again at some point. Thus it make sense to create a class that opens the file in the constructor, and closes the file again in the destructor.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> FileAccess
<span style="color: #008000;">&#123;</span>
  FileAccess<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Open file...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
  ~FileAccess<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Close file...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Read and write operations are similar functionality (transfer data, but in opposite direction) and thus it makes sense to collect this functionality in one class. As the goal is to modify the contents of a file, the reading of the existing file content can be placed in the constructor, and writing of the modified content in the destructor.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> FileManipulate
<span style="color: #008000;">&#123;</span>
  FileManipulate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Read from file...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
  ~FileManipulate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Write to file...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// Content manipulation functions follows here...</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Two classes are now at hand. One that opens and closes a file, and one that reads the contents of the file and writes it back to the file.</p>
<p>A prerequisite of reading from a file or writing to a file is that the file is open. Therefore the basic but essential functionality of opening and closing is made the base class (<code>FileAccess</code>). The more advanced and flexible functionality of reading and writing is then made in the derived class (<code>FileManipulate</code>).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Base <span style="color: #ff0000; font-style: italic;">/* FileAccess */</span>
<span style="color: #008000;">&#123;</span>
  Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Open file...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
  ~Base<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Close file...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Derived <span style="color: #ff0000; font-style: italic;">/* FileManipulate */</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Base <span style="color: #ff0000; font-style: italic;">/* FileAccess */</span>
<span style="color: #008000;">&#123;</span>
  Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Read from file...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
  ~Derived<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Write to file...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Thus every time an object is created of the derived class for modifying a files contents, it automagically also inherits the capabilities of opening and closing files. And as a file cannot be read from before the file is opened, it can be remembered that the base constructor must be called before the derived constructor. Equally it cannot be written to the file after the file is closed, thus is can be remembered that the derived destructor is called before the base destructor.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2007/09/19/c-contructordestructor-call-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

