<?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>Alex on Linux</title>
	<atom:link href="http://www.alexonlinux.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.alexonlinux.com</link>
	<description></description>
	<lastBuildDate>Tue, 14 Aug 2012 19:24:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>printf() vs stream IO in C++</title>
		<link>http://www.alexonlinux.com/printf-vs-stream-io-in-c</link>
		<comments>http://www.alexonlinux.com/printf-vs-stream-io-in-c#comments</comments>
		<pubDate>Tue, 14 Aug 2012 19:10:51 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[Short articles]]></category>
		<category><![CDATA[c programming language]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[output stream]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[standard library]]></category>
		<category><![CDATA[stream]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=2018</guid>
		<description><![CDATA[Before joining Dell I was mostly working in kernel writing in C programming language. At Dell I still work on mostly low level stuff, but this time it is user-mode, so I am not tied up to C anymore. We&#8217;re writing in C++ and I am learning C++. One of the less appealing things for [...]<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://www.alexonlinux.com/how-less-processes-its-input' rel='bookmark' title='How less processes its input'>How less processes its input</a></li>
<li><a href='http://www.alexonlinux.com/cc-reference-counting-with-atomic-variables-and-gcc' rel='bookmark' title='C/C++ reference counting with atomic variables and gcc'>C/C++ reference counting with atomic variables and gcc</a></li>
<li><a href='http://www.alexonlinux.com/a-new-kind-of-virtualization' rel='bookmark' title='A new kind of virtualization'>A new kind of virtualization</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Before joining Dell I was mostly working in kernel writing in C programming language. At Dell I still work on mostly low level stuff, but this time it is user-mode, so I am not tied up to C anymore. We&#8217;re writing in C++ and I am learning C++. One of the less appealing things for me in C++ was streaming support and the way its input/output implemented. In particular I got used to printf() functions family and leaving those in favor of streams and cout was tough. What really strikes me is the fact that no C++ book explains this stuff. All C++ books just tell you &#8211; so, my dear, this is how this stuff is done in C++. It took me some time to realize how C++ style input/output is much more convenient and powerful than printf() family of functions. Here&#8217;s why.<br />
<span id="more-2018"></span></p>
<h3>The C++ way</h3>
<p>So, In C++ you normally work with stream of some sort. Stream is an abstraction that may hide anything in the world. It can be string stream that writes things to string or debug output stream that sends debug printouts to some remote debug log server. Once you have a stream, you redefine operator &lt;&lt; for various classes to write into stream instead of doing bit-shift. For input streams you define operator &gt;&gt; that reads data from string or from socket and initializes object of some class with values it read. All those operator &lt;&lt; and &gt;&gt; seems kind of weird. And in fact, this can be any of the operators. For some reason, standard library uses bit-shift operators and this turned into a standard. Boost, popular C++ extensions library uses operator% to implement similar functionality, although in slightly different way.</p>
<h3>Convenience</h3>
<p>At first, defining operator &lt;&lt; for every class I write seemed like a tedious task. But you just do it once and then you can use it forever. Moreover, you can use classes that already have it to implement more stuff. So you just do it once really.</p>
<h3>It makes much more sense in object oriented language</h3>
<p>Since we&#8217;re talking object oriented here, C++ way to output stuff actually makes much more sense. The way object represented as a string is a property is the object itself.</p>
<h3>Performance</h3>
<p>Here is the interesting part. In printf(), the standard C library has to parse format string every time you print something. So, at runtime, every time you print something, standard C library parses your format string to figure out what and how many parameters you passed to printf(). This is important for high performance systems. Intensively printing, just anything, whether this is a debug log or part of some communication protocol rises CPU consumption of your program to the roof.</p>
<blockquote><p>I worked for one company where they came up with a really neat trick to overcome the problem of high CPU consumption. They had to implement debug logging in C for their high performance product. So, instead of formatting the string every time there is a printout, they saved format string and then every printout they just saved the arguments in their binary form. To read the debug log, they had to read format strings that were periodically saved in the debug log file. Then they read binary arguments for the debug printout and finally formatted the message. So instead of formatting the printouts at runtime, they formatted the printouts when debug log is read.</p></blockquote>
<p>Another problem occurs when you by mistake pass wrong data type to printf() function and compiler misses it. Another problem is when you pass a string that is not properly null terminated. Etc. Etc. C++ way solves all these problems. First, data type resolving happens during compilation. It is up to compiler to detect what type of data you&#8217;re trying to print and to call right operator&lt;&lt;. This improves CPU consumption situation. For the same reason, it is much harder to print something that is of unexpected type. Compiler is usually pretty good at resolving data types.</p>
<h3>Wrapping it up</h3>
<p>So, it seems that C++ way of producing output both better conforms object oriented way of programming and yields higher quality results. The only problem is that it requires you to get used to, but then, you do it just once.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/how-less-processes-its-input' rel='bookmark' title='How less processes its input'>How less processes its input</a></li>
<li><a href='http://www.alexonlinux.com/cc-reference-counting-with-atomic-variables-and-gcc' rel='bookmark' title='C/C++ reference counting with atomic variables and gcc'>C/C++ reference counting with atomic variables and gcc</a></li>
<li><a href='http://www.alexonlinux.com/a-new-kind-of-virtualization' rel='bookmark' title='A new kind of virtualization'>A new kind of virtualization</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/printf-vs-stream-io-in-c/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>gcc macro language extensions</title>
		<link>http://www.alexonlinux.com/gcc-macro-language-extensions</link>
		<comments>http://www.alexonlinux.com/gcc-macro-language-extensions#comments</comments>
		<pubDate>Wed, 08 Feb 2012 22:28:41 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[concatenation]]></category>
		<category><![CDATA[debug macro]]></category>
		<category><![CDATA[define]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[macros with variable number of arguments]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[stringification]]></category>
		<category><![CDATA[stringify]]></category>
		<category><![CDATA[token]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[variadic macros]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1976</guid>
		<description><![CDATA[One of the great things about gcc and in particular its C/C++ preprocessor is various extensions that it has. In this post I would like to briefly describe three of them. One allows to turn C/C++ token into a string. Here token is anything that you can pass as an argument to a macro. Second allows you [...]<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://www.alexonlinux.com/pthread_exit-in-c' rel='bookmark' title='pthread_exit() in C++'>pthread_exit() in C++</a></li>
<li><a href='http://www.alexonlinux.com/my-next-programming-language' rel='bookmark' title='My next programming language'>My next programming language</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>One of the great things about gcc and in particular its C/C++ preprocessor is various extensions that it has. In this post I would like to briefly describe three of them. One allows to turn C/C++ token into a string. Here token is anything that you can pass as an argument to a macro. Second allows you concatenate two tokens to create new expression. The last one allows C/C++ macros with variable number of arguments.<br />
<span id="more-1976"></span><br />
<h1>Stringifying a token</h1>
<p>Its amazing how useful this is. Take following code for example.</p>
<pre class="brush:cpp">std::cout &lt;&lt; "obj.member1: " &lt;&lt; obj.member1 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member2: " &lt;&lt; obj.member2 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member3: " &lt;&lt; obj.member3 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member4: " &lt;&lt; obj.member4 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member5: " &lt;&lt; obj.member5 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member6: " &lt;&lt; obj.member6 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member7: " &lt;&lt; obj.member7 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member8: " &lt;&lt; obj.member8 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member9: " &lt;&lt; obj.member9 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member10: " &lt;&lt; obj.member10 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member11: " &lt;&lt; obj.member11 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member12: " &lt;&lt; obj.member12 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member13: " &lt;&lt; obj.member13 &lt;&lt; std::endl;
std::cout &lt;&lt; ", obj.member14: " &lt;&lt; obj.member14 &lt;&lt; std::endl;</pre>
<p>Wouldn&#8217;t you give a kidney just not to write name of every single member of <code>obj</code> twice? Well, it appears that this can be done. Watch this:</p>
<pre class="brush:cpp">#define PMEM(mem) #mem ": " &lt;&lt; mem
#define PCMEM(mem) ", " #mem ": " &lt;&lt; mem</pre>
<p>Now you can do the following:</p>
<pre class="brush:cpp">std::cout &lt;&lt; PMEM(obj.member1) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member2) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member3) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member4) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member5) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member6) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member7) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member8) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member9) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member10) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member11) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member12) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member13) &lt;&lt; std::endl;
std::cout &lt;&lt; PCMEM(obj.member14) &lt;&lt; std::endl;</pre>
<p>These two macros will do most of the job for you. Unfortunately, they cannot write the code for you, so you will have to write names of members of <code>obj</code> at least once. # operator does one simple thing. Whatever you use it on turns into a string. Just in case you&#8217;re wondering, I am using here another gcc&#8217;s feature &#8211; string concatenation. gcc allows you to take two immediate strings and concatenate them. First I turned expression <code>obj.member1</code> into a string using # operator # and then I concatenated it with <code>": ".</code> Note that stringification of tokens only works inside of macro. Writing something like this:</p>
<pre class="brush:cpp">std::cout &lt;&lt; #some_token &lt;&lt; std::endl;</pre>
<p>will produce compilation error and for a good reason. Another interesting thing is the fact that you can turn anything into a string, even if it is not a valid C/C++ expression. Take a look at the code below:</p>
<pre class="brush:cpp">#define DPRINT(a) #a
std::cout &lt;&lt; DPRINT(a + b) &lt;&lt; std::endl;
std::cout &lt;&lt; DPRINT(hello world) &lt;&lt; std::endl;</pre>
<p>This code will print two strings, first is <code>a + b</code> and second is <code>hello world</code>. This is despite the fact that <code>hello world</code> is not a valid C/C++.</p>
<h1>Token concatenation</h1>
<p>Using this feature you can construct new C/C++ tokens using existing tokens. For instance, if you have a large structure and you want to write a function for every member of the structure. One way to do that is by writing the code manually. But I guess you don&#8217;t need me for that.</p>
<pre class="brush:cpp">struct some_struct {
    int member1;
    bool member2;
    unsigned long member3;
}

#define ADD_GETTER(TYPE, MEMBER) \
    TYPE get_ ## MEMBER(struct some_struct&amp; st) { \
        return st.MEMBER; \
}

ADD_GETTER(int, member1);
ADD_GETTER(bool, member2);
ADD_GETTER(unsigned long, member3);</pre>
<p>Lets analyze this piece of code for second. First I defined a structure called <code>some_struct</code>. Next, I wanted a macro that defines getter function for every member of <code>some_struct</code>. I added <code>ADD_GETTER</code> macro for that. Then I called it three times in a row providing type of the field in <code>some_struct</code> and name of the member.</p>
<p>Calling a macro for member1 expanded to following piece of code:</p>
<pre class="brush:cpp">int get_member1(struct some_struct&amp; st) {
    return st.member1;
}</pre>
<p>Notice how it created name of the function. This is concatenation operation in action. ## makes gcc and g++ preprocessor concatenate two tokens, <code>get_</code> and <code>member1</code> into single token. ## operator removes all space characters between two tokens. Another thing that it does is eliminating white space and punctuation characters between two tokens. This is especially useful when implementing macros with variable number of arguments.</p>
<h1>Macros with variable number of arguments</h1>
<p>You can define a macro with variable number of arguments following way:</p>
<pre class="brush:cpp">#define VMACRO(argument1, argument2, ...) do_something()</pre>
<p>The three dots as last argument of the macro tells compiler that this is a variadic macro. I.e. this is a macro that receives variable number of arguments. To get access to arguments, you have to use special keyword __VA_ARGS__. Like this:</p>
<pre class="brush:cpp">#define VMACRO(argument1, argument2, ...) do_something(__VA_ARGS__)</pre>
<p>In this example I am ignoring <code>argument1</code> and <code>argument2</code> and passing remaining arguments to <code>do_something()</code> routine. When I first learned about this feature, I immediately tried to use it for debug printouts macros. This is the code that I&#8217;ve written.</p>
<pre class="brush:cpp">#include &lt;stdio.h&gt;

#define DPRINT(format, ...) printf("DEBUG: " format, __VA_ARGS__) 

int main()
{
    DPRINT("hello world");
}</pre>
<p>Note that strings have to be immediate values. For instance calling <code>DPRINT(format, "...");</code> where <code>format</code> is pointer to string will not work because gcc cannot concatenate <code>format</code> with &#8220;DEBUG&#8221; string. Anyway, I wanted to address something different. You will be surprised to learn that this code doesn&#8217;t compile. This is because after preprocessing this code turns into something that is not valid C/C++. This is how main will look like after preprocessing:</p>
<pre class="brush:cpp">int main()
{
    printf("DEBUG: " "hello world", );
}</pre>
<p>Note the comma character after &#8220;hello world&#8221;. The thing is that empty token is valid token in gcc, so passing nothing as argument translates into nothing. There is a workaround for this problem. That is using concatenation operation. Lets change our implementation of DPRINT a little.</p>
<pre class="brush:cpp">#include &lt;stdio.h&gt;

#define DPRINT(format, ...) printf("DEBUG: " format, ##__VA_ARGS__) 

int main()
{
    DPRINT("hello world");
}</pre>
<p>Note concatenation operator before <code>__VA_ARGS__</code>. I already mentioned that concatenation operator gets rid of white space and punctuation characters between two tokens. This is exactly what it does in this case &#8211; it removes comma between format and empty token leaving clean <code>printf("DEBUG: " "hello world");</code> This is exactly what we needed.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/pthread_exit-in-c' rel='bookmark' title='pthread_exit() in C++'>pthread_exit() in C++</a></li>
<li><a href='http://www.alexonlinux.com/my-next-programming-language' rel='bookmark' title='My next programming language'>My next programming language</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/gcc-macro-language-extensions/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>UML cheatsheet</title>
		<link>http://www.alexonlinux.com/uml-cheatsheet</link>
		<comments>http://www.alexonlinux.com/uml-cheatsheet#comments</comments>
		<pubDate>Mon, 19 Sep 2011 16:48:44 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code library]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[Short articles]]></category>
		<category><![CDATA[cheatsheet]]></category>
		<category><![CDATA[class diagram]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[UML]]></category>
		<category><![CDATA[uml diagram]]></category>
		<category><![CDATA[uml reference]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1945</guid>
		<description><![CDATA[Every once in awhile, I have to draw a UML diagram. I rarely do serious designs with UML, however sometimes I do need to depict some piece of code in a diagram and UML seems to be the best notation around. Unfortunately, various sources of information on UML tend to over-complicate things. I am not software architect [...]<div class='yarpp-related-rss yarpp-related-none'>

No related posts.
</div>
]]></description>
				<content:encoded><![CDATA[<p>Every once in awhile, I have to draw a UML diagram. I rarely do serious designs with UML, however sometimes I do need to depict some piece of code in a diagram and UML seems to be the best notation around.</p>
<p>Unfortunately, various sources of information on UML tend to over-complicate things. I am not software architect and drawing UMLs is not my job. So my UML skills are poor by definition. Moreover, I am happy with this situation and don&#8217;t see it changing in the future (even if I get promoted <img src='http://www.alexonlinux.com/wp-content/plugins/smilies-themer/modern/wink.gif' alt=';-)' class='wp-smiley' /> ).</p>
<p>So from time to time I need a simple UML reference card. Simple search finds references <a href="http://www.holub.com/goodies/uml/" onclick="return TrackClick('http%3A%2F%2Fwww.holub.com%2Fgoodies%2Fuml%2F','like+this+one')">like this one</a>, which are excellent if you are serious about UML, and I am not.</p>
<p>Eventually, I decided to write a short UML class diagram reference card for myself. I hope you will enjoy it as well.<br />
<span id="more-1945"></span></p>
<h2>Inheritance</h2>
<p><a href="http://www.alexonlinux.com/wp-content/uploads/2011/09/child_inherits_from_parent.png" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2011%2F09%2Fchild_inherits_from_parent.png','child_inherits_from_parent')"><img class="size-full wp-image-1952 aligncenter" title="child_inherits_from_parent" src="http://www.alexonlinux.com/wp-content/uploads/2011/09/child_inherits_from_parent.png" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2011%2F09%2Fchild_inherits_from_parent.png','child_inherits_from_parent')" alt="" width="456" height="103" /></a></p>
<p>So, this is how classes inherit one from another. Here <em>Child class</em> inherits from <em>Parent Class</em>.</p>
<h2>Use</h2>
<p><a href="http://www.alexonlinux.com/wp-content/uploads/2011/09/user_uses_resource.png" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2011%2F09%2Fuser_uses_resource.png','user_uses_resource')"><img class="size-full wp-image-1953 aligncenter" title="user_uses_resource" src="http://www.alexonlinux.com/wp-content/uploads/2011/09/user_uses_resource.png" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2011%2F09%2Fuser_uses_resource.png','user_uses_resource')" alt="" width="445" height="104" /></a></p>
<p>This is how <em>User class</em> uses <em>Resource class</em>.</p>
<h2>Contains and manages</h2>
<p><a href="http://www.alexonlinux.com/wp-content/uploads/2011/09/whole_part_managed.png" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2011%2F09%2Fwhole_part_managed.png','whole_part_managed')"><img class="size-full wp-image-1957 aligncenter" title="whole_part_managed" src="http://www.alexonlinux.com/wp-content/uploads/2011/09/whole_part_managed.png" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2011%2F09%2Fwhole_part_managed.png','whole_part_managed')" alt="" width="429" height="107" /></a></p>
<p>Here, <em>Whole</em> class contains and manages <em>Part </em>class. This type of relation can be extended to one of the following:</p>
<ul>
<li>One to One</li>
<li>One to Many</li>
<li>Many to One</li>
<li>Many to Many</li>
</ul>
<h2>References</h2>
<p><a href="http://www.alexonlinux.com/wp-content/uploads/2011/09/whole_part_unmanaged.png" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2011%2F09%2Fwhole_part_unmanaged.png','whole_part_unmanaged')"><img class="size-full wp-image-1959 aligncenter" title="whole_part_unmanaged" src="http://www.alexonlinux.com/wp-content/uploads/2011/09/whole_part_unmanaged.png" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2011%2F09%2Fwhole_part_unmanaged.png','whole_part_unmanaged')" alt="" width="382" height="103" /></a></p>
<p>Here, <em>Whole</em> class references to <em>Part</em> class, but does not manage it. Again, can be extended with:</p>
<ul>
<li>One to One</li>
<li>One to Many</li>
<li>Many to One</li>
<li>Many to Many</li>
</ul>
<p>&nbsp;</p>
<p>This is enough information for now. I&#8217;ll probably extend it over time. In any case, please post your corrections and suggestions.</p>
<div class='yarpp-related-rss yarpp-related-none'>
<p>No related posts.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/uml-cheatsheet/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Making writes durable &#8211; is your data on disk?</title>
		<link>http://www.alexonlinux.com/making-writes-durable-is-your-data-on-disk</link>
		<comments>http://www.alexonlinux.com/making-writes-durable-is-your-data-on-disk#comments</comments>
		<pubDate>Mon, 19 Sep 2011 16:26:03 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[Short articles]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1942</guid>
		<description><![CDATA[Here is an interesting article written by Evan Jones. The article explains how you can be guaranteed when your data is on disk. In case you&#8217;re wondering, when write(), fwrite() or any other library call that writes data to disk reports success you are not guaranteed that the data is actually on the disk. In [...]<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://www.alexonlinux.com/what-is-direct-io-anyway' rel='bookmark' title='What is direct I/O anyway?'>What is direct I/O anyway?</a></li>
<li><a href='http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables' rel='bookmark' title='Multithreaded simple data type access and atomic variables'>Multithreaded simple data type access and atomic variables</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.evanjones.ca/durable-writes.html" onclick="return TrackClick('http%3A%2F%2Fwww.evanjones.ca%2Fdurable-writes.html','Here+is')">Here is</a> an interesting article written by <a href="http://www.evanjones.ca/" onclick="return TrackClick('http%3A%2F%2Fwww.evanjones.ca%2F','Evan+Jones')">Evan Jones</a>. The article explains how you can be guaranteed when your data is on disk.</p>
<p>In case you&#8217;re wondering, when write(), fwrite() or any other library call that writes data to disk reports success you are not guaranteed that the data is actually on the disk. In fact, in Linux, write() reports success when data is in dirty cache. Then, special kernel thread kicks in and makes sure that the data is on disk.</p>
<p>Depending on circumstances, it may take some time until writer kernel thread will finish writing. Anyway, in his post Evan talks about how to make sure that the data is actually stable on disk.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/what-is-direct-io-anyway' rel='bookmark' title='What is direct I/O anyway?'>What is direct I/O anyway?</a></li>
<li><a href='http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables' rel='bookmark' title='Multithreaded simple data type access and atomic variables'>Multithreaded simple data type access and atomic variables</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/making-writes-durable-is-your-data-on-disk/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Models for multithreaded applications</title>
		<link>http://www.alexonlinux.com/models-for-multithreaded-applications</link>
		<comments>http://www.alexonlinux.com/models-for-multithreaded-applications#comments</comments>
		<pubDate>Thu, 21 Apr 2011 22:00:18 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[Short articles]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[scheduler]]></category>
		<category><![CDATA[synchronization]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1776</guid>
		<description><![CDATA[As you know, I changed a couple of workplaces during my career. Long story short, one interesting thing that I noticed in different companies is various models for multi-threaded programs (mostly for large embedded systems). Lets say we have a multi-threaded program.  The program should handle heterogeneous tasks. On a very basic level, our program [...]<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://www.alexonlinux.com/cc-reference-counting-with-atomic-variables-and-gcc' rel='bookmark' title='C/C++ reference counting with atomic variables and gcc'>C/C++ reference counting with atomic variables and gcc</a></li>
<li><a href='http://www.alexonlinux.com/pthread-mutex-vs-pthread-spinlock' rel='bookmark' title='pthread mutex vs pthread spinlock'>pthread mutex vs pthread spinlock</a></li>
<li><a href='http://www.alexonlinux.com/a-new-kind-of-virtualization' rel='bookmark' title='A new kind of virtualization'>A new kind of virtualization</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>As you know, I changed a couple of workplaces during my career. Long story short, one interesting thing that I noticed in different companies is various models for multi-threaded programs (mostly for large embedded systems).</p>
<p><span id="more-1776"></span>Lets say we have a multi-threaded program.  The program should handle heterogeneous tasks. On a very basic level, our program probably receives some data, processes it and produces an output. Question of how to manage threads usually arises in performance critical applications. Otherwise, it is quiet simple &#8211; we squeeze into single thread as many tasks as possible.</p>
<p>Actual data processing may take several steps. We may want to split it to several tasks, each handled by thread of its own.</p>
<h2>Thread per task model</h2>
<p>When using this model, we allocate single thread per task. Since we want to pass work from one thread to another, we will need some synchronization mechanism.</p>
<p>I suppose we&#8217;ll need some queues to pass things between threads. Also, we may need muteces or semaphores to synchronize access to queues and other shared objects.</p>
<p>One obvious weakness of this approach is performance. When moving task objects from one thread to another we often move them from CPU to CPU. Modern CPUs heavily deploy caching mechanisms. So keeping objects local to one CPU brings significant performance boost. By moving task from one CPU to another we heart caching and cause performance degradation.</p>
<p>Another performance related problem is that while your task object moves from one thread to another it should undergo at least one context switch. Depending on overall number of tasks, time it takes to process single task can be greater. Another cause of context switches is mutual exclusion mechanism that have to protect the data structure used to communicate objects between threads.</p>
<p>One last significant performance issue that I would like to mention is in the core of this approach. Each thread is bound by processing power of a single core. But what if one of the tasks require more processing power. The computer may have enough processing power. This is especially true with recent multi-core trend. But when a single thread runs on one core at a time inevitably each thread processing the request, is a potential bottle-neck.</p>
<p>Advantage of this approach is in its simplicity. You don&#8217;t have to develop significant infrastructure to make it work. All you need is pthreads and a couple of classes that do the job. All tools that you will use are available out of the box. Because it is so common, it will be easier to find skilled and experienced engineers to implement and use with this model.</p>
<h2>Worker threads</h2>
<p>In this model, each task split into multiple jobs. Each job being handled by single thread. There is number of worker threads each taking care of at most single job at any particular moment. To improve performance, system tries to handle jobs that belong to same task on the same core. This means that there might be number of threads per single core.</p>
<p>Unlike thread per task model, this model is much more difficult to implement. It implies some constraints on the tasks:</p>
<ol>
<li>Engineer designing such system should be able to split tasks into multiple jobs. Alternatively, tasks can be very short.</li>
<li>Jobs should be more or less equal in size in terms of required processing resources.</li>
</ol>
<p>In addition, this approach requires smart task management. Such system should implement scheduler that spreads tasks between cores. A serios problem arises when number of running tasks becomes larger than number of cores in the system. When it happens, scheduler should start two or more tasks on single core. To do it wisely, it should be aware of how much processing power required by tasks. It should manage number of tasks running on particular core and assign new task to least busy core.</p>
<p>Needless to say that this approach is way more complex than naive thread per task model. It requires more skilled engineers. Moreover, these engineers should spend their time developing something that is not directly related to the matters of the project &#8211; that is various infrastructures for the project.</p>
<p>On the other hand, this approach allows engineers to squeeze every bit of performance out of the machine. This approach will take the hardware to its limit.</p>
<h2>Other approaches</h2>
<p>There are many other approaches of course. Most of them however, are an attempt to merge between two models I described. For instance, one company started with thread per task model for their embedded system and quickly ran into performance problems. So they tried to solve problems by splitting busiest tasks into multiple threads, basically changing threading model for one particular thread into worker threads model.</p>
<p>Recently I ran into another model that is somewhat unique. This model implements so called user-mode threads. Entire program runs in a single thread. Like worker threads model, in this model tasks split into smaller jobs. However, instead of letting the operating system to schedule between job execution, this model implements a user-mode scheduler.</p>
<p>The actual scheduling is made using setjmp() and longjmp() calls. One obvious constrain that this model presents is that if thread goes to sleep in operating system terms it will send entire system to sleep. As a result, the system should have very tight control over what threads does and in particular how it sleeps. I.e. threads are not allowed to do regular I/O. Instead they should do I/O in a centralized way.</p>
<p>For example, thread that wishes to read from a file-descriptor should register this file-descriptor with scheduler. Scheduler will select() or poll() on all registered file-descriptors. Depending on what file-descriptor requires attention, scheduler will schedule thread that takes care of that file-descriptor.</p>
<p>In this model you can run only a single instance of scheduler on single processor. So this model is not entierly optimized for MP machines. If you still want to run multiple schedulers on single computer then you can either run them in separate processes or multiple threads of the same process. In both cases communication between threads that belong to different schedulers is cumbersome. For instance if run multiple schedulers in multiple threads, you cannot use mutexes to synchronize between threads that belong to various schedulers because locking such mutex will send entire system to sleep. Instead you should use some sort of I/O based IPC.</p>
<p>Despite number of disadvantages that this approach has, there are couple of good things into it. Synchronizing between threads that belong to same instance of scheduler is much easier because there is, at most, one thread that runs at any particular moment. So, if, for instance, you have some simple data structure that being used by multiple threads there is no need to protect it from simultaneous access.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/cc-reference-counting-with-atomic-variables-and-gcc' rel='bookmark' title='C/C++ reference counting with atomic variables and gcc'>C/C++ reference counting with atomic variables and gcc</a></li>
<li><a href='http://www.alexonlinux.com/pthread-mutex-vs-pthread-spinlock' rel='bookmark' title='pthread mutex vs pthread spinlock'>pthread mutex vs pthread spinlock</a></li>
<li><a href='http://www.alexonlinux.com/a-new-kind-of-virtualization' rel='bookmark' title='A new kind of virtualization'>A new kind of virtualization</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/models-for-multithreaded-applications/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Python for bash replacement</title>
		<link>http://www.alexonlinux.com/python-for-bash-replacement</link>
		<comments>http://www.alexonlinux.com/python-for-bash-replacement#comments</comments>
		<pubDate>Sun, 26 Dec 2010 16:25:37 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[Short articles]]></category>
		<category><![CDATA[System Administrator Articles]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[ipython]]></category>
		<category><![CDATA[piping]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[redirections]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1847</guid>
		<description><![CDATA[When I started learning Python, I was looking for a programming language that would replace BASH, AWK and SED. I am a C/C++ programmer and as such I better invest my time into studying C and C++. Instead, every time I needed some complex script I opened up a book on BASH and refreshed my [...]<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://www.alexonlinux.com/6-things-that-i-miss-in-bash' rel='bookmark' title='6 things that I miss in bash'>6 things that I miss in bash</a></li>
<li><a href='http://www.alexonlinux.com/pythons-optparse-for-human-beings' rel='bookmark' title='Python&#8217;s optparse for human beings'>Python&#8217;s optparse for human beings</a></li>
<li><a href='http://www.alexonlinux.com/my-next-programming-language' rel='bookmark' title='My next programming language'>My next programming language</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>When I started learning Python, I was looking for a programming language that would replace BASH, AWK and SED. I am a C/C++ programmer and as such I better invest my time into studying C and C++. Instead, every time I needed some complex script I opened up a book on BASH and refreshed my knowledge. And since bumping into boundaries of what BASH can do is relatively easy, I always opened awk/sed book few minutes later.</p>
<p>Actually, this is quiet common. Once in a while I see my colleagues, just like myself, open up a book on BASH. The problem is that because we don&#8217;t actively program BASH, the knowledge and experience that we gain from this experience wear out over time. So next time we approach, so we have to repeatedly study BASH stuff over and over again. And again, this is not only BASH I am talking about, but also AWK and SED.</p>
<p>It is utterly broken state of affairs and I wish there was a solution. Unfortunately there is no solution yet. The good thing is that with some effort the solution may arise. I am talking about Python programming language.</p>
<p><span id="more-1847"></span></p>
<p>Needless to say that Python can do everything that BASH can do. However, it was never designed as shell environment and as such it is hardly convenient.</p>
<p>For instance, running external commands is easy. You should use subprocess module to run the command and there you go, you have both return value and the output. But this is miles away from convenience of simply typing the command and hitting enter.</p>
<p>Another issue is input/output redirections and piping. Regular shell does this with ease. Python obviously does this as well, but not without a hassle of importing the right module and creating the right object, etc.</p>
<p>On the other hand things are not that bad. For instance implementing command line should be somewhat easy in Python because it has native support for completion (which it uses in its own command line interface).</p>
<p>There is a project that makes few steps in the right direction. I am talking about IPython of course. Unfortunately, I doubt that IPython will ever be able to replace BASH. It was never their goal. Even though IPython developers implemented some features that make IPython command line a little down to earth, I am in doubt they will continue moving in this direction. Also, I must say that IPython evolves quiet slowly.</p>
<p>What do you think? Will Python ever be able to replace BASH?</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/6-things-that-i-miss-in-bash' rel='bookmark' title='6 things that I miss in bash'>6 things that I miss in bash</a></li>
<li><a href='http://www.alexonlinux.com/pythons-optparse-for-human-beings' rel='bookmark' title='Python&#8217;s optparse for human beings'>Python&#8217;s optparse for human beings</a></li>
<li><a href='http://www.alexonlinux.com/my-next-programming-language' rel='bookmark' title='My next programming language'>My next programming language</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/python-for-bash-replacement/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>pthread_exit() in C++</title>
		<link>http://www.alexonlinux.com/pthread_exit-in-c</link>
		<comments>http://www.alexonlinux.com/pthread_exit-in-c#comments</comments>
		<pubDate>Sat, 09 Oct 2010 09:28:03 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[Short articles]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[pthread]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[unwinding]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1841</guid>
		<description><![CDATA[Today I ran into an interesting problem that I would like to share. I am working on multi-threaded code in C++. Here&#8217;s what happened. I started a thread that looks like this: try { do_something() } catch (...) { std::cout &#60;&#60; "Got unknown exception" &#60;&#60; std::endl; } The do_something() routine eventually called pthread_exit(). Once I [...]<div class='yarpp-related-rss yarpp-related-none'>

No related posts.
</div>
]]></description>
				<content:encoded><![CDATA[<p>Today I ran into an interesting problem that I would like to share. I am working on multi-threaded code in C++. Here&#8217;s what happened.</p>
<p>I started a thread that looks like this:</p>
<pre class="brush:cpp;">try {
    do_something()
} catch (...) {
    std::cout &lt;&lt; "Got unknown exception" &lt;&lt; std::endl;
}</pre>
<p>The do_something() routine eventually called pthread_exit().</p>
<p>Once I ran this piece of code, I instantly got an unknown exception notification. Long story short, here&#8217;s what I found out.</p>
<p>When calling pthread_exit() in C++, it has to destruct all objects that has been created on stack. This process called stack unwinding and this is exactly what happens when you throw an exception. pthread_exit() utilizes this feature of C++ to cleanup before shutting down the thread for good.</p>
<p>To do that pthread_exit() throws some obscure exception and catches it right before ditching the thread. This way it cleans up all objects nicely. On the other hand, catching &#8230; becomes impossible.</p>
<div class='yarpp-related-rss yarpp-related-none'>
<p>No related posts.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/pthread_exit-in-c/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My next programming language</title>
		<link>http://www.alexonlinux.com/my-next-programming-language</link>
		<comments>http://www.alexonlinux.com/my-next-programming-language#comments</comments>
		<pubDate>Sun, 22 Aug 2010 21:04:43 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[bazaar]]></category>
		<category><![CDATA[CVS]]></category>
		<category><![CDATA[evolution]]></category>
		<category><![CDATA[future]]></category>
		<category><![CDATA[mercurial]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming language]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[version control system]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1819</guid>
		<description><![CDATA[This week-end I&#8217;ve been playing with various version control systems. Until now, I&#8217;ve been doing all my home codings with subversion. I&#8217;ve written<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://www.alexonlinux.com/distributed-vs-centralized-version-control-systems' rel='bookmark' title='Distributed vs. centralized version control systems'>Distributed vs. centralized version control systems</a></li>
<li><a href='http://www.alexonlinux.com/bazaar-for-subversion-users-part-1-the-basics' rel='bookmark' title='Bazaar for subversion users, part 1 &#8211; the basics'>Bazaar for subversion users, part 1 &#8211; the basics</a></li>
<li><a href='http://www.alexonlinux.com/todo-list' rel='bookmark' title='Todo list'>Todo list</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>This week-end I&#8217;ve been playing with various version control systems. Until now, I&#8217;ve been doing all my home codings with subversion. I&#8217;ve written <a title="Link to article about bazaar" href="bazaar-for-subversion-users-part-1-the-basics" onclick="return TrackClick('bazaar-for-subversion-users-part-1-the-basics" onclick="return TrackClick('bazaar-for-subversion-users-part-1-the-basics','Link+to+article+about+bazaar')",'Link+to+article+about+bazaar')">about bazaar in the past</a>, but it seems to me that <a href="http://stackoverflow.com/questions/995636/popularity-of-git-mercurial-bazaar-vs-which-to-recommend">bazaar isn&#8217;t going anywhere</a> and it busts any piece of motivation that I have to continue writing about it.</p>
<p>Version control that I did try is git. This is a very popular version control system and for a good reason. Comparing git and subversion brought me to a conclusion that git is really a very consequence of how things work in the world. Thing about git is that it is a distributed version control while subversion is not. You can make a distributed version of subversion, but it won&#8217;t be subversion anymore.</p>
<p>Distributivity is one thing, but there are more. Take the standard trunk/branches/tags layout that you have to create in subversion &#8211; version control could do it for you, as git/bazaar/mercurial do. At first, after working with CVS for some time, having an option to have non-standard layout seemed cool for some time. But then it appeared completely useless.</p>
<p>This brings a notion of evolution into version control systems and I bet there&#8217;s similar process with programming languages. I think we can already starting drawing a programming language that will replace Python.</p>
<p>Yep, Python isn&#8217;t perfect. I thought I&#8217;d compile a list of things that shall be different in ought to be programming language that comes after Python.</p>
<p><span id="more-1819"></span></p>
<ol>
<li>Passing <em>self</em> to methods is counter-productive. Using <em>self</em> to access members is super counter productive. Yet this is how Python works. Without this, it won&#8217;t be Python anymore.</li>
<li>Same thing with <em>global</em>. Every program has this one global variable that every other class and function use. So, every entity that uses that variable has to declare it with <em>global</em> keyword. Utterly counter-productive.</li>
<li>Starting every private method and field with __ (double under-score) is ugly. The way it changes method name prepending it with a class name is even uglier. It is a really hackish way to handle this problem. I understand that this is the Python way of doing things, but it feels wrong.</li>
<li>Another hackish thing are decorators. Make an option to have static methods, class methods and properties and decorators are gone. No one would need them anymore, and it will be one bit easier to master Python.</li>
<li>You name it.</li>
</ol>
<p><em>Disclaimer (can&#8217;t get along without one this time <img src='http://www.alexonlinux.com/wp-content/plugins/smilies-themer/modern/smile.gif' alt=':-)' class='wp-smiley' /> ): This post may seem like one criticizing Python and it does in some ways, but I doubt these can be fixed in something called Python (unless Guido van Rossum brews it and calls it P</em><em>ython 4). In the meantime I love Python and use it on a daily basis. </em></p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/distributed-vs-centralized-version-control-systems' rel='bookmark' title='Distributed vs. centralized version control systems'>Distributed vs. centralized version control systems</a></li>
<li><a href='http://www.alexonlinux.com/bazaar-for-subversion-users-part-1-the-basics' rel='bookmark' title='Bazaar for subversion users, part 1 &#8211; the basics'>Bazaar for subversion users, part 1 &#8211; the basics</a></li>
<li><a href='http://www.alexonlinux.com/todo-list' rel='bookmark' title='Todo list'>Todo list</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/my-next-programming-language/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Call a constructor or allocate an object in-place</title>
		<link>http://www.alexonlinux.com/call-a-constructor-or-allocate-an-object-in-place-2</link>
		<comments>http://www.alexonlinux.com/call-a-constructor-or-allocate-an-object-in-place-2#comments</comments>
		<pubDate>Sun, 04 Jul 2010 20:40:10 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[Short articles]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[new]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1803</guid>
		<description><![CDATA[Since I joined Dell, my main field of research and work has somewhat changed. Now I am mostly working with C++ and file-systems. This world is not entirely new to me, but apparently I have a lot of stuff to learn. Today I&#8217;d like to talk about one nice trick that I learned few days [...]<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href='http://www.alexonlinux.com/direct-io-in-python' rel='bookmark' title='Direct IO in Python'>Direct IO in Python</a></li>
<li><a href='http://www.alexonlinux.com/printf-vs-stream-io-in-c' rel='bookmark' title='printf() vs stream IO in C++'>printf() vs stream IO in C++</a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Since I joined Dell, my main field of research and work has somewhat changed. Now I am mostly working with C++ and file-systems. This world is not entirely new to me, but apparently I have a lot of stuff to learn.</p>
<p>Today I&#8217;d like to talk about one nice trick that I learned few days ago.</p>
<p>When working with large software systems, memory management becomes an imperative. In C, you can easily allocate a large chunk of memory and allocate structure right on that buffer. This is by far more difficult in C++, because compiler has to call consturctor.</p>
<p>Apparently, you can, in a way, directly call object&#8217;s constructor . I.e. you can allocate an object, on specified memory region, without actually allocating this region.</p>
<p>This is how you do it.</p>
<pre class="brush:cpp">char* s = new char[1024];

SomeClass* p = new (s) SomeClass;
</pre>
<p>First new operator just allocates 1024 bytes. This is good old allocation as we know it. Note the special syntax of the second new operator. It allocates the new object on memory specified in brackets. Basically, this calls SomeClass&#8217;s constructor using s as storage.</p>
<p><em>One thing that I don&#8217;t know how to do is how to call destructor on the object &#8211; i.e. how to delete an object in place.</em></p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/direct-io-in-python' rel='bookmark' title='Direct IO in Python'>Direct IO in Python</a></li>
<li><a href='http://www.alexonlinux.com/printf-vs-stream-io-in-c' rel='bookmark' title='printf() vs stream IO in C++'>printf() vs stream IO in C++</a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/call-a-constructor-or-allocate-an-object-in-place-2/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>This is me, couple of weeks ago.</title>
		<link>http://www.alexonlinux.com/this-is-me-couple-of-weeks-ago</link>
		<comments>http://www.alexonlinux.com/this-is-me-couple-of-weeks-ago#comments</comments>
		<pubDate>Tue, 11 May 2010 16:25:29 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1792</guid>
		<description><![CDATA[I think this is going to become my new icon. What do you think?<div class='yarpp-related-rss yarpp-related-none'>

No related posts.
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.alexonlinux.com/wp-content/uploads/2010/05/me.jpg" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2010%2F05%2Fme.jpg','me')"><img class="aligncenter size-full wp-image-1791" title="me" src="http://www.alexonlinux.com/wp-content/uploads/2010/05/me.jpg" onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fwp-content%2Fuploads%2F2010%2F05%2Fme.jpg','me')" alt="" width="598" height="398" /></a></p>
<p>I think this is going to become my new icon. What do you think?</p>
<div class='yarpp-related-rss yarpp-related-none'>
<p>No related posts.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/this-is-me-couple-of-weeks-ago/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
