<?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; C++</title>
	<atom:link href="http://monzool.net/blog/category/programming/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://monzool.net/blog</link>
	<description>a/ Jan Skriver Sørensen</description>
	<lastBuildDate>Wed, 09 May 2012 19:37:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</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>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>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>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>
		<item>
		<title>Placement new</title>
		<link>http://monzool.net/blog/2007/08/20/placement-new/</link>
		<comments>http://monzool.net/blog/2007/08/20/placement-new/#comments</comments>
		<pubDate>Mon, 20 Aug 2007 21:15:48 +0000</pubDate>
		<dc:creator>monzool</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://monzool.net/blog/2007/08/20/placement-new/</guid>
		<description><![CDATA[REPLACEMENT NEW CAME to mind, when working on a C++ project of mine. In this project some large objects of short lifetime led to a concerns about memory fragmentation on the targeted embedded platform that have only sparse memory ressources. The life cycle of the objects was to create an object, then fill the large [...]]]></description>
			<content:encoded><![CDATA[<p><strong>REPLACEMENT NEW CAME</strong> to mind, when working on a C++ project of mine. In this project some large objects of short lifetime led to a concerns about memory fragmentation on the targeted embedded platform that have only sparse memory ressources. The life cycle of the objects was to create an object, then fill the large data member structures, store data for a while until time for transmitting the data and terminate the object. The object data where always different so they could not be reused, and the lifetime of each object was inditerminable. However it was certain that only a known maximum amount of objects would exist at the same time.</p>
<p>This senario made me think of a rarely used memory allocation functionality called <code>placement new</code> that would handle the above concerns perfectly. With this variant of <code>new</code> you allocate a memory pool of fixed memory address and size, from which allocations can be made from. This way <code>placement new</code> allows reuse of the same memory again and again and thus containing any memory fragmentation within the disposable memory pool.</p>
<p>Below is a simple example of how to use the placement new functionality.</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
</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;iomanip&gt;</span>
<span style="color: #339900;">#include &lt;new&gt;</span>
<span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> C
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
            C<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> data<span style="color: #008000;">&#40;</span><span style="color: #208080;">0x11</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
            ~C<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> data <span style="color: #000080;">=</span> <span style="color: #208080;">0x22</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #0000ff;">char</span> data<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</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>
  <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> PoolItems <span style="color: #000080;">=</span> <span style="color: #0000dd;">6</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> PoolSize  <span style="color: #000080;">=</span> PoolItems <span style="color: #000040;">*</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>C<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Allocate a memory pool</span>
  <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>pool <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#91;</span>PoolSize<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">memset</span><span style="color: #008000;">&#40;</span>pool, <span style="color: #208080;">0x00</span>, PoolSize<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Create some pointers to access pooled objects</span>
  C <span style="color: #000040;">*</span>pC<span style="color: #008000;">&#91;</span>PoolItems<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Allotate objects at &quot;arbitary&quot; addresses in the pool</span>
  pC<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>pool <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>C<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> C<span style="color: #008080;">;</span>
  pC<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>pool <span style="color: #000040;">+</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>C<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> C<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Display constructed data</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> PoolSize<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> setfill<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">'0'</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> setw<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">hex</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>pool<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Explicit destruction of objects in pool</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> PoolItems<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pC<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      pC<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>~C<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// Explicit destructor call</span>
      pC<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// Display destructed data</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> PoolSize<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> setfill<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">'0'</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> setw<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">hex</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>pool<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// Release memory in pool</span>
  <span style="color: #0000dd;">delete</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pool<span style="color: #008080;">;</span>
  pool <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</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></p>
<p>Explanation: First a memory pool is created that fits a fixed amount of the designated class objects. Two object are then created within pool slots 1 and 4. The simple byte sized class <code>C</code> has a contructor initialization of the member data.</p>
<p>The first printing of the pool will display the following contents:
<pre>00 11 00 00 11 00</pre>
<p> This indicates that two slots are allocated with objects of the class <code>C</code>.</p>
<p>Destructing the objects are done by calling the destructor explicitly, NOT by normal delete action (as the memory pool original was created as a char array, the memory it occupies is tagged as a char array that must be deleted the approriate array delete operation). After destruction, the pool contents is displayed again, and it is seen that the same memory has been rewritten by the destructor assigning:
<pre>00 22 00 00 22 00</pre>
<p>Finally the memory pool is deleted and the memory is released.</p>
]]></content:encoded>
			<wfw:commentRss>http://monzool.net/blog/2007/08/20/placement-new/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

