﻿<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
  <channel>
    <title>Stev.Org</title>
    <description>Random Stuff from a software developer</description>
    <link>http://www.stev.org/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 2.0.0.36</generator>
    <language>en-GB</language>
    <blogChannel:blogRoll>http://www.stev.org/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>James</dc:creator>
    <dc:title>Stev.Org</dc:title>
    <geo:lat>0.000000</geo:lat>
    <geo:long>0.000000</geo:long>
    <item>
      <title>C++ - Prevent an object by being copied with boost::noncopyable</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Sometimes in C++ it can be an advantage for making a class noncopyable for various reasons. This can be acomplished by inherting a class from the boost libraries. Which is called boost::noncopyable. This will force the classes copy constructors to be private.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;class MyClass : private boost::noncopyable
{
	public:
		MyClass() { }
};&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;By doing this. It will prevent the following from being able to compile because a copy is passed to the function. A reference should be passed instead. This will also prevent operations such as "a = b".&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;void func(MyClass a)
{

}

int main(int argc, char **argv)
{
	MyClass a, b;
	
	func(a);

	return 0;
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Some advantages of using this include.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px;"&gt;Boost::noncopyable prevents the classes methods from accidentally using the private copy constructor&lt;/span&gt;&lt;/li&gt;
&lt;li style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; background-position: initial initial; background-repeat: initial initial;"&gt;It is more explicit and descriptive in the intent. Using private copy functions is an idiom that takes longer to spot than&amp;nbsp;&lt;code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"&gt;noncopyable&lt;/code&gt;.&lt;/li&gt;
&lt;li style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; background-position: initial initial; background-repeat: initial initial;"&gt;It is less code / less typing / less clutter / less room for error (the easiest would be accidentally providing an implementation).&lt;/li&gt;
&lt;li style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; background-position: initial initial; background-repeat: initial initial;"&gt;You don't have to write your own private copy constructor.&lt;/li&gt;
&lt;li style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; background-position: initial initial; background-repeat: initial initial;"&gt;Attempting to copy will catch issues sooner. eg at compile time.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;One disadvantage is that there is a small overheat of the class inheirtance.&lt;/p&gt;</description>
      <link>http://www.stev.org/post/2013/08/22/C++-Prevent-an-object-by-being-copied-with-boostnoncopyable.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/08/22/C++-Prevent-an-object-by-being-copied-with-boostnoncopyable.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=0f821a86-f1bf-44ff-967a-cd0b43659ec4</guid>
      <pubDate>Thu, 22 Aug 2013 07:00:00 +0100</pubDate>
      <category>C++</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=0f821a86-f1bf-44ff-967a-cd0b43659ec4</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=0f821a86-f1bf-44ff-967a-cd0b43659ec4</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/08/22/C++-Prevent-an-object-by-being-copied-with-boostnoncopyable.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=0f821a86-f1bf-44ff-967a-cd0b43659ec4</wfw:commentRss>
    </item>
    <item>
      <title>Bash - Upgrading lots of git repos.</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is a nice example of how to execute a git command on multiple git repositories one after another. Assuming that they are all in the same directory.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;pre class="brush: plain;"&gt;#!/bin/bash

for i in * ; do
	if [ -d $i ] ; then
		if [ -d $i/.git ] ; then
			echo Updating $i ....
			(cd $i ; git pull)
			echo
		fi
	fi
done&lt;/pre&gt;
&lt;/p&gt;</description>
      <link>http://www.stev.org/post/2013/08/16/Bash-Upgrading-lots-of-git-repos.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/08/16/Bash-Upgrading-lots-of-git-repos.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=63ffeecb-a628-456d-8aa7-9f23365c294d</guid>
      <pubDate>Fri, 16 Aug 2013 19:33:00 +0100</pubDate>
      <category>Bash</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=63ffeecb-a628-456d-8aa7-9f23365c294d</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=63ffeecb-a628-456d-8aa7-9f23365c294d</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/08/16/Bash-Upgrading-lots-of-git-repos.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=63ffeecb-a628-456d-8aa7-9f23365c294d</wfw:commentRss>
    </item>
    <item>
      <title>Pyton - How to find out if a key exists in a dictionary</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Another simple python example which is how to find out if a key exists in python&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Just use "if" and "in" like in the following example&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: plain;"&gt;&amp;gt;&amp;gt;&amp;gt; x['item1'] = 0
&amp;gt;&amp;gt;&amp;gt; if 'item1' in x:
...  print "True"
...
True&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Some of the mistakes that I have seen is this will "appear" to work correctly but in fact it does not. Have a look at these examples. You can see here that it will fail because it is testing the value rather than the key.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: plain;"&gt;&amp;gt;&amp;gt;&amp;gt; x['item1'] = 1
&amp;gt;&amp;gt;&amp;gt; if x['item1']:
...  print "True"
...
True
&amp;gt;&amp;gt;&amp;gt;

&amp;gt;&amp;gt;&amp;gt; x['item1'] = 0
&amp;gt;&amp;gt;&amp;gt; if x['item1']:
...  print "True"
...
&amp;gt;&amp;gt;&amp;gt;&lt;/pre&gt;</description>
      <link>http://www.stev.org/post/2013/04/09/Pyton-How-to-find-out-if-a-key-exists-in-a-dictionary.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/04/09/Pyton-How-to-find-out-if-a-key-exists-in-a-dictionary.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=da987b13-f804-4357-b52b-70da7d8799d6</guid>
      <pubDate>Tue, 09 Apr 2013 08:00:00 +0100</pubDate>
      <category>Python</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=da987b13-f804-4357-b52b-70da7d8799d6</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=da987b13-f804-4357-b52b-70da7d8799d6</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/04/09/Pyton-How-to-find-out-if-a-key-exists-in-a-dictionary.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=da987b13-f804-4357-b52b-70da7d8799d6</wfw:commentRss>
    </item>
    <item>
      <title>Python - json encode / decode</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is a short example to be able to encode / decode json inside python. Its actually really easy to do.&lt;/p&gt;
&lt;p&gt;You will first need to import the json library using&lt;/p&gt;
&lt;p&gt;import json&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Encoding to json&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: plain;"&gt;&amp;gt;&amp;gt;&amp;gt; x = {}
&amp;gt;&amp;gt;&amp;gt; x['1'] = 0
&amp;gt;&amp;gt;&amp;gt; x['2'] = 0
&amp;gt;&amp;gt;&amp;gt; x['3'] = 0
&amp;gt;&amp;gt;&amp;gt; x['4'] = 0
&amp;gt;&amp;gt;&amp;gt; json.dumps(x)&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Deocoding from json&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: plain;"&gt;&amp;gt;&amp;gt;&amp;gt; x = json.loads('{"1": 0, "2": 0, "3": 0, "4": 0}')
&amp;gt;&amp;gt;&amp;gt; print x
{u'1': 0, u'3': 0, u'2': 0, u'4': 0}
&amp;gt;&amp;gt;&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It really is as simple as that!&lt;/p&gt;</description>
      <link>http://www.stev.org/post/2013/04/04/Python-json-encode-decode.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/04/04/Python-json-encode-decode.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=f1fa7b07-2f5a-4fed-a3aa-08caed5d70cf</guid>
      <pubDate>Thu, 04 Apr 2013 08:00:00 +0100</pubDate>
      <category>Python</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=f1fa7b07-2f5a-4fed-a3aa-08caed5d70cf</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=f1fa7b07-2f5a-4fed-a3aa-08caed5d70cf</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/04/04/Python-json-encode-decode.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=f1fa7b07-2f5a-4fed-a3aa-08caed5d70cf</wfw:commentRss>
    </item>
    <item>
      <title>C++ - std::vector add, insert, remove, process, erase, swap, sort, clear</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is some examples of how to use std::vector with string for most of the common operations that can be performed on std::vector.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following example assume the following has been declared&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;std::vector&amp;lt;std::string&amp;gt; vec;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Adding Items&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;vec.push_back("Item 1");
vec.push_back("Item 2");
vec.push_back("Item 3");
vec.push_back("Item 4");
vec.push_back("Item 5");&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Adding Items to front&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;vec.insert(vec.begin(), "Front 2");
vec.insert(vec.begin(), "Front 1");&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Remove Last Item&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;vec.pop_back();&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Remove Last Item&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;vec.erase(vec.end());&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Remove First Item&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;vec.erase(vec.begin());&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Process All Items&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;for(std::vector&amp;lt;std::string&amp;gt;::iterator it = vec.begin(); it != vec.end(); it++)
{
  printf("%s\n", it-&amp;gt;c_str());
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Process All Items&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;for(size_t i = 0; i &amp;lt; vec.size(); i++)
{
  printf("%s\n", vec[i].c_str());
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Reverse The Order&lt;/strong&gt;&lt;/span&gt; - Note you can also just run the above backwards instead.&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;for(size_t i = 0; i &amp;lt; vec.size() / 2; i++)
{
  std::string tmp = vec[i];
  vec[i] = vec[vec.size() - i - 1];
  vec[vec.size() - i - 1] = tmp;
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;&lt;span style="white-space: pre;"&gt;S&lt;/span&gt;orting&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;std::sort(vec.begin(), vec.end());&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Find and Remove an item&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;for(std::vector&amp;lt;std::string&amp;gt;::iterator it = vec.begin(); it != vec.end(); it++)
{
  if (*it == "Item 2")
  {
    vec.erase(it);
    break;	//it is now invalud must break!
  }
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;Clear All Items&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;vec.clear();&lt;/pre&gt;</description>
      <link>http://www.stev.org/post/2013/04/02/C++-stdvector-.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/04/02/C++-stdvector-.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=efd8ccd2-aef7-4b35-80d2-0324157dfd97</guid>
      <pubDate>Tue, 02 Apr 2013 08:00:00 +0100</pubDate>
      <category>C++</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=efd8ccd2-aef7-4b35-80d2-0324157dfd97</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=efd8ccd2-aef7-4b35-80d2-0324157dfd97</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/04/02/C++-stdvector-.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=efd8ccd2-aef7-4b35-80d2-0324157dfd97</wfw:commentRss>
    </item>
    <item>
      <title>C++ - Why you need a copy constructor</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In C++ you may come across an issues where you need to use a copy constructor. This is because when a class is assigned from one variable to another it has to copy the values across. If we are dealing with pointers that are inside the class then the pointer will be copied. So this can create and issue if the pointer is deallocated in the destructor. Since now there is two copied of the same pointer which is going to be deallocated twice. This will result in a common issue such as a double free.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In this short program you can see the effect here that will cause a double free. In this case I am using "malloc" to keep the example short. The following program will crash when the main function returns as it will call free from the destructor twice on the same pointer value.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;class MyClass {
    public:
        char *buf;
		size_t buf_len;

        MyClass(size_t len) {
            printf("Alloc\n");
			buf_len = len;
            buf = (char *) malloc(len * sizeof(buf));
        }

        ~MyClass() {
            printf("Free\n");
            free(buf);
        }
};


int main(int argc, char **argv) {
    MyClass x = MyClass(4096);
    MyClass y = x;

    return 0;
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To get another the issue above in C++ we must use a copy constructor which will allow us to override the default functionality when the class is copied in order to allocate and copy the contents of the buffer and a new pointer which will completly seperate the source class from the new class that is being assigned.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

class MyClass {
    public:
        char *buf;
		size_t buf_len;

        MyClass(size_t len) {
            printf("Alloc\n");
			buf_len = len;
            buf = (char *) malloc(len * sizeof(buf));
        }

        ~MyClass() {
            printf("Free\n");
            free(buf);
        }
        
        MyClass(const MyClass &amp;amp;source)
        {
        	buf_len = source.buf_len;
        	buf = (char *) malloc(buf_len * sizeof(buf));
        	memcpy(buf, source.buf, buf_len);
        }
};


int main(int argc, char **argv) {
    MyClass x = MyClass(4096);
    MyClass y = x;

    return 0;
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>
      <link>http://www.stev.org/post/2013/03/29/C++-Why-you-need-a-copy-constructor.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/03/29/C++-Why-you-need-a-copy-constructor.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=479d1833-cf90-445b-b14f-cdd701028d43</guid>
      <pubDate>Fri, 29 Mar 2013 10:29:00 +0100</pubDate>
      <category>C++</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=479d1833-cf90-445b-b14f-cdd701028d43</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=479d1833-cf90-445b-b14f-cdd701028d43</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/03/29/C++-Why-you-need-a-copy-constructor.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=479d1833-cf90-445b-b14f-cdd701028d43</wfw:commentRss>
    </item>
    <item>
      <title>C++ - Boost Optional</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using boost::optional can be an advantage in certain situation. In C++ it effectivly works by being able to turn any datatype into a nullable value. It works much the same way as returning a null pointer to an object but without using a pointer. Instead this will work by being able to return an object which may or may not valid.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It is typically used to return a value from a function where the function may fail in certain situations. If you attempt to use the optional value which has not been set it will fail and the program will abort. This of course is also useful for debugging in order to catch issues sooner rather than later by accessing various variable that may not have valid values.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It can of course also be used to pass optional paramaters to functions where you may not want to use operator overloading.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is an example of it converting an string to an int which will obviously fail on one of the strings.&lt;/p&gt;
&lt;div&gt;
&lt;pre class="brush: cpp;"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string&amp;gt;

#include &amp;lt;boost/optional.hpp&amp;gt;

boost::optional&amp;lt;int&amp;gt; func(const std::string &amp;amp;str)
{
        boost::optional&amp;lt;int&amp;gt; value;
        int tmp = 0;

        if (sscanf(str.c_str(), "%d", &amp;amp;tmp) == 1)
                value.reset(tmp);

        return value;
}

int main(int argc, char **argv)
{
        boost::optional&amp;lt;int&amp;gt; v1 = func("31245");
        boost::optional&amp;lt;int&amp;gt; v2 = func("hello");

        if (v1)
                printf("%d\n", v1.get());
        else
                printf("v1 not valid\n");

        if (v2)
                printf("%d\n", v2.get());
        else
                printf("v2 not valid\n");

        return 0;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;The output of the above is&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;pre class="brush: plain;"&gt;31245
v2 not valid&lt;/pre&gt;
&lt;/div&gt;</description>
      <link>http://www.stev.org/post/2013/03/27/C++-Boost-Optional.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/03/27/C++-Boost-Optional.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=496dacda-fc91-4b30-9647-36f06db5e511</guid>
      <pubDate>Wed, 27 Mar 2013 17:21:00 +0100</pubDate>
      <category>C++</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=496dacda-fc91-4b30-9647-36f06db5e511</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=496dacda-fc91-4b30-9647-36f06db5e511</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/03/27/C++-Boost-Optional.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=496dacda-fc91-4b30-9647-36f06db5e511</wfw:commentRss>
    </item>
    <item>
      <title>Linux - What and how to kill a zombie process</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&lt;strong&gt;What is a zombie?&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;A zombie process is simply a process that has exited but its parent has not yet read the processes exit code. Since the process has exited it does not have any open files or uses any memory in fact the only resources it is using is a entry in the task list. So in most Linux systems will mean that it is using around 4k-8k of memory.&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;Until the parent process reads the exit code the zombie will exist. Typically this points to a software bug in the parent process or an issue that caused the child (which is now the zombie) to exit unexpectedly. Which would also point to a bug or issue in the program. In either case the parent process should have actually dealt with the error an acted on it to read the exit status of the process which will clean up the zombie process and allow it to exit.&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&lt;strong&gt;Why is there any issue?&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;Since the program isn't really using any resources it really doesn't cause any issues. However if you have a lot of zombie's being generated inside a system due to a bug in the software it will cause an issue because it is still running as a process and sooner or later you will hit the limit of the maximum number of processes permitted to run in the system. If this limit is reached you will not be able to start any more processes. This means you won't be able to run commands from the command line any more.&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&lt;strong&gt;Why does kill now work on the zombie process?&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;Kill simply does not work on a zombie process since it is in fact already "dead"&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&lt;strong&gt;How to kill it?&lt;/strong&gt;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;Since the issue really exists in the parent process the solution is to remove the parent process either by killing it or shutting it down. This will cause the child "zombie" processes to get a new parent process which will be the parent of the parent.&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;If you are working in a complex system you may need to kill several parent process in order to eliminate the zombies until the zombies get a parent process that will read the exit code from the processes which will cause the zombie process to be remove.&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;Hint: if you use the command "ps axfu" the "f" in the argument list will print the output as a tree which will make finding the parent somewhat simpler.&lt;/p&gt;</description>
      <link>http://www.stev.org/post/2013/03/01/Linux-What-and-how-to-kill-a-zombie-process.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/03/01/Linux-What-and-how-to-kill-a-zombie-process.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=b425da52-0868-4839-8155-836b5564bbae</guid>
      <pubDate>Fri, 01 Mar 2013 10:12:00 +0100</pubDate>
      <category>Linux</category>
      <category>Linux Tips</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=b425da52-0868-4839-8155-836b5564bbae</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=b425da52-0868-4839-8155-836b5564bbae</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/03/01/Linux-What-and-how-to-kill-a-zombie-process.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=b425da52-0868-4839-8155-836b5564bbae</wfw:commentRss>
    </item>
    <item>
      <title>C - Some Simple Examples of using strcat</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This post shows some simple example of using strcat to join two strings together in C&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The msot common example you will find on the internet will work by just making an array that is "big enough" to store both the strings. However this can create problems when the strings you use are too long so it would need these conditions handled correctly in C. So it probably isn't the best example to use.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;void basic(char *s1, char *s2)
{
	char str[200] = "";
	
	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;A better example of using strcat is to allocate an array dynamically. In this case on the stack. So this will work slightly better however it can cause a significant amount of stack space to be used and can also cause crashes if the strings are too long. However it will execute very quickly because dynamic allocation of memory on the stack will perform well.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;void better1(char *s1, char *s2)
{
	char str[strlen(s1) + strlen(s2) * sizeof(*s1) + 1];
	str[0] = '\0';

	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;A much better example of this is to use malloc / free for the allocation of string. This will be slightly slower than the previoud example but will be able to handler much larger strings and much safer to use. It can of course still crash but only if the machine run's out of memory. How ever this error can be handled to prevent it crashing by changing the call to abort() to use proper error handling.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;void better2(char *s1, char *s2)
{
	char *str = malloc(strlen(s1) + strlen(s2) + 1 * sizeof(*s1));
	if (str == NULL)
		abort();
	str[0] = '\0';
	
	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
		
	free(str);
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Finally this is an example about joining multiple strings so we can just keep making our string longer and longer or join it from multiple different sources. Note that the following code really needs a check around the realloc function to see if it returns NULL. If it does then it should fail by returning the previous pointer. Or call abort because the system has run out of memory.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;char *multiple(char *str, char *s2)
{
	int len;
	char *s;
	if (str != NULL)
		len = strlen(str);
	len += strlen(s2) + 1 * sizeof(*s2);
	s = realloc(str, len);
	strcat(s, s2);
	return s;
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the above code we can call it in the following way&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: cpp;"&gt;char *tmp = multiple(NULL, "Hello ");
	tmp = multiple(tmp, " World");
	tmp = multiple(tmp, " We");
	tmp = multiple(tmp, " Can");
	tmp = multiple(tmp, " Join");
	tmp = multiple(tmp, " strings");
	printf("%s\n", tmp);
	free(tmp);&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>
      <link>http://www.stev.org/post/2013/02/16/C-Some-Simple-Examples-of-using-strcat.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/02/16/C-Some-Simple-Examples-of-using-strcat.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=028c358e-dd34-47e1-b5a9-195fb898ee1b</guid>
      <pubDate>Sat, 16 Feb 2013 11:06:00 +0100</pubDate>
      <category>C</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=028c358e-dd34-47e1-b5a9-195fb898ee1b</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=028c358e-dd34-47e1-b5a9-195fb898ee1b</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/02/16/C-Some-Simple-Examples-of-using-strcat.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=028c358e-dd34-47e1-b5a9-195fb898ee1b</wfw:commentRss>
    </item>
    <item>
      <title>Debian - Getting sshfs to work</title>
      <description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is a short guide to get ssh to work on debian. So this should also work with some other linux varients like ubuntu. It can be a very useful tool to be able to access your home directory from a 2nd linux machine or from your raspberry pi. I have been using it so that you can run your normal powerful code editors which the raspberry pi does not have enough resources to run them fast enough.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-weight: bold; font-family: Calibri; font-size: 11.0pt;"&gt;To Install / Setup&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;sudo apt-get install sshfs&lt;span style="font-size: 11pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;sudo addgroup $USER fuse&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;Note: You don't need to add your self to the fuse ground. But it is required if you want to be able to mount / umount the filesystem without root permissions.&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-weight: bold; font-size: 11pt;"&gt;To Use&lt;/p&gt;
&lt;p style="margin: 0in; font-size: 11pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-size: 11pt;"&gt;mkdir Raspberry&lt;/p&gt;
&lt;p style="margin: 0in; font-size: 11pt;"&gt;sshfs &amp;lt;username&amp;gt;@&amp;lt;ip address&amp;gt;:/home/&amp;lt;username&amp;gt;/Raspberry&amp;nbsp; -o uid=1001,gid=1002 Raspberry&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-size: 11pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-size: 11pt;"&gt;Note: in the above you should change the uid and gid to your user id and group id. You can look thoose up in the /etc/passwd and /etc/group files on the machine that you are mounting the remote filesystem on.&lt;/p&gt;
&lt;p style="margin: 0in; font-size: 11pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-size: 11pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-weight: bold; font-family: Calibri; font-size: 11.0pt;"&gt;To unmount&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;fusermount -u /home/james/Raspberry/&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"&gt;Another useful tip is that it will also work with &lt;a href="http://www.stev.org/post/2012/12/08/Linux-ssh-key-authentication.aspx"&gt;ssh key authentication&lt;/a&gt;&amp;nbsp;so that you do not even require a password or so that you can have it configured to automount on boot using the crontab.&lt;/p&gt;</description>
      <link>http://www.stev.org/post/2013/02/14/Debian-Getting-sshfs-to-work.aspx</link>
      <author>james@stev.org</author>
      <comments>http://www.stev.org/post/2013/02/14/Debian-Getting-sshfs-to-work.aspx#comment</comments>
      <guid>http://www.stev.org/post.aspx?id=15544a22-a1b0-4d4e-a91c-46e15adb2b39</guid>
      <pubDate>Thu, 14 Feb 2013 20:23:00 +0100</pubDate>
      <category>Linux</category>
      <category>Linux Tips</category>
      <category>Debian</category>
      <category>Ubuntu</category>
      <dc:publisher>james</dc:publisher>
      <pingback:server>http://www.stev.org/pingback.axd</pingback:server>
      <pingback:target>http://www.stev.org/post.aspx?id=15544a22-a1b0-4d4e-a91c-46e15adb2b39</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.stev.org/trackback.axd?id=15544a22-a1b0-4d4e-a91c-46e15adb2b39</trackback:ping>
      <wfw:comment>http://www.stev.org/post/2013/02/14/Debian-Getting-sshfs-to-work.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.stev.org/syndication.axd?post=15544a22-a1b0-4d4e-a91c-46e15adb2b39</wfw:commentRss>
    </item>
  </channel>
</rss>