<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Unmaintainable &#187; c/c++</title>
	<atom:link href="http://unmaintainable.wordpress.com/category/cc/feed/" rel="self" type="application/rss+xml" />
	<link>http://unmaintainable.wordpress.com</link>
	<description>Scripting, Software Engineering and Stuff in Between</description>
	<lastBuildDate>Sun, 22 Nov 2009 09:05:47 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='unmaintainable.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/6f90ae5619dfc90140df401ac60575d2?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Unmaintainable &#187; c/c++</title>
		<link>http://unmaintainable.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://unmaintainable.wordpress.com/osd.xml" title="Unmaintainable" />
		<item>
		<title>Simple Error Handling in C</title>
		<link>http://unmaintainable.wordpress.com/2007/09/27/error-handling-in-c/</link>
		<comments>http://unmaintainable.wordpress.com/2007/09/27/error-handling-in-c/#comments</comments>
		<pubDate>Thu, 27 Sep 2007 19:26:56 +0000</pubDate>
		<dc:creator>mafr</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://unmaintainable.wordpress.com/2007/09/27/error-handling-in-c/</guid>
		<description><![CDATA[Currently I&#8217;ve got the pleasure to do some coding in C. There&#8217;s nothing wrong with that, but things can get a bit uncomfortable for those spoilt by languages like Python or Java. So it&#8217;s nice to have a library of useful functions from various areas, which I accumulated over the years.

One of them is err_quit(). [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unmaintainable.wordpress.com&blog=586265&post=30&subd=unmaintainable&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Currently I&#8217;ve got the pleasure to do some coding in C. There&#8217;s nothing wrong with that, but things can get a bit uncomfortable for those spoilt by languages like Python or Java. So it&#8217;s nice to have a library of useful functions from various areas, which I accumulated over the years.</p>
<p><span id="more-30"></span></p>
<p>One of them is <code>err_quit()</code>. It provides a convenient way to exit the program after printing an error message, a pattern often used in command line tools. The function supports a format string and a variable number of arguments, just like the <code>printf(3)</code> family of functions. And the best thing: It&#8217;s all ANSI C compliant.</p>
<pre>
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdarg.h&gt;

void err_quit(const char *fmt, ...) {
        va_list ap;
        va_start(ap, fmt);

        vfprintf(stderr, fmt, ap);

        va_end(ap);

        exit(EXIT_FAILURE);
}
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/unmaintainable.wordpress.com/30/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/unmaintainable.wordpress.com/30/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/unmaintainable.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/unmaintainable.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/unmaintainable.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/unmaintainable.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/unmaintainable.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/unmaintainable.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/unmaintainable.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/unmaintainable.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/unmaintainable.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/unmaintainable.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unmaintainable.wordpress.com&blog=586265&post=30&subd=unmaintainable&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://unmaintainable.wordpress.com/2007/09/27/error-handling-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/59c9677a3b9569af44561adab6c2a980?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mafr</media:title>
		</media:content>
	</item>
		<item>
		<title>Understanding Function Pointers in C</title>
		<link>http://unmaintainable.wordpress.com/2007/04/13/understanding-function-pointers-in-c/</link>
		<comments>http://unmaintainable.wordpress.com/2007/04/13/understanding-function-pointers-in-c/#comments</comments>
		<pubDate>Fri, 13 Apr 2007 10:01:30 +0000</pubDate>
		<dc:creator>mafr</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://unmaintainable.wordpress.com/2007/04/13/understanding-function-pointers-in-c/</guid>
		<description><![CDATA[Many people familiar with object oriented programming languages like Java seem to have trouble understanding C function pointers. Even among C programmers, they are sometimes treated like an obscure feature that is avoided wherever possible. However, if you understand the concept of a pointer and know a few basic rules, they are pretty easy to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unmaintainable.wordpress.com&blog=586265&post=17&subd=unmaintainable&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Many people familiar with object oriented programming languages like Java seem to have trouble understanding C function pointers. Even among C programmers, they are sometimes treated like an obscure feature that is avoided wherever possible. However, if you understand the concept of a pointer and know a few basic rules, they are pretty easy to use.</p>
<p><span id="more-17"></span></p>
<p>Most often, function pointers are used to pass user-defined behavior to library functions. The library function can then use the pointer to execute the function the pointer refers to (in object oriented languages, this is a typical use case for the strategy design pattern). A common example is the standard library&#8217;s <code>qsort(3)</code> function:</p>
<pre>
  void qsort(void *base, size_t nmemb, size_t size,
                    int (*compar)(const void *, const void *));
</pre>
<p>In its last argument, it accepts a function pointer which compares two elements in the array <code>base</code>. Since the array could contain virtually anything, it&#8217;s up to the caller to provide a comparison function to determine the order of elements. The syntax of <code>qsort()</code>&#8217;s third argument looks a bit strange, but we&#8217;ll get to back to it later.</p>
<p>Let&#8217;s start with a few basics first. Consider the following function:</p>
<pre>
  int foo(char *str, int val) {
    printf("%s: %d\n", str, val);
    return val+1;
  }
</pre>
<p>Getting a pointer to this function is easy: It is simply the name of this function, <em>foo</em>. Executing the function this pointer refers to is trivial, it&#8217;s the standard function call syntax:</p>
<pre>
  int ret;
  ret = foo("Hello World", 41);
</pre>
<p>Things get more interesting if you want to store this function pointer in a variable. To declare a variable, you need its type, but what&#8217;s the type of a function pointer?</p>
<p>Fortunately, you can derive it from the function&#8217;s signature easily. Take the signature and replace the function&#8217;s name with <code>(*NAME)</code>, where NAME is the variable&#8217;s name:</p>
<pre>
  int (*my_fptr)(char *str, int val);
</pre>
<p>Note that this variable is only suitable for storing pointers to functions with this exact signature! It is also important to make sure the variable&#8217;s name and the asterisk are enclosed by parenthesis.</p>
<p>You can now assign function <code>foo</code> to <code>my_fptr</code> and run the function it points too:</p>
<pre>
  my_fptr = foo;
  my_fptr("Test", 7);
</pre>
<p>From the assignment on, <code>foo</code> and <code>my_fptr</code> can be used interchangeably (no dereferencing necessary!), with the exception that you can assign <code>my_fptr</code> a different function pointer later, of course.</p>
<p>To simplify variable declaration, we can use a type definition. The syntax is a bit counter-intuitive, but this is how it works:</p>
<pre>
  typedef int (*fptr_t)(char *str, int val);
</pre>
<p>The variable declaration looks much simpler, now that we have defined the type <code>fptr_t</code>:</p>
<pre>
  fptr_t my_fptr;
</pre>
<p>Looking back to the <code>qsort()</code> example, we now understand how the last argument works. It tells us (and the compiler) that <code>qsort()</code> expects a pointer to a function which returns an <code>int</code> and expects two arguments of type <code>const void *</code>. The name of the argument is <code>compar</code>, and <code>compar(x, y)</code> is the syntax that is used by <code>qsort()</code> to call the user-provided function.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/unmaintainable.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/unmaintainable.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/unmaintainable.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/unmaintainable.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/unmaintainable.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/unmaintainable.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/unmaintainable.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/unmaintainable.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/unmaintainable.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/unmaintainable.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/unmaintainable.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/unmaintainable.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unmaintainable.wordpress.com&blog=586265&post=17&subd=unmaintainable&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://unmaintainable.wordpress.com/2007/04/13/understanding-function-pointers-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/59c9677a3b9569af44561adab6c2a980?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mafr</media:title>
		</media:content>
	</item>
		<item>
		<title>Testing C++ applications using CppUnit</title>
		<link>http://unmaintainable.wordpress.com/2006/12/13/testing-c-apps-using-cppunit/</link>
		<comments>http://unmaintainable.wordpress.com/2006/12/13/testing-c-apps-using-cppunit/#comments</comments>
		<pubDate>Wed, 13 Dec 2006 06:30:13 +0000</pubDate>
		<dc:creator>mafr</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[build systems]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://unmaintainable.wordpress.com/2006/12/13/testing-c-apps-using-cppunit/</guid>
		<description><![CDATA[Unit testing is hip these days and every programming language has its own JUnit clone that mimics the original more or less closely. For C++, there&#8217;s the excellent CppUnit package that I&#8217;ve been using extensively lately. Unfortunately, C++ is less dynamic than languages like Java, so you can&#8217;t simply tell the framework to execute all [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unmaintainable.wordpress.com&blog=586265&post=5&subd=unmaintainable&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Unit testing is hip these days and every programming language has its own <a href="http://junit.org">JUnit</a> clone that mimics the original more or less closely. For C++, there&#8217;s the excellent <a href="http://cppunit.sourceforge.net">CppUnit</a> package that I&#8217;ve been using extensively lately. Unfortunately, C++ is less dynamic than languages like Java, so you can&#8217;t simply tell the framework to execute all methods who&#8217;s names start with &#8220;test&#8221;. With CppUnit and C++, you have to write a fair amount of boiler plate code yourself to register test cases and their methods.</p>
<p><span id="more-5"></span></p>
<p>To get you started, I published my <a href="http://users.musicbrainz.org/~matt/instant-cppunit-0.1.tar.gz">Instant-CppUnit package</a>. All you have to do is to extract it in your project&#8217;s root directory and adjust the Makefile that comes with it. The package includes a template with examples, a test runner (taken from CppUnit&#8217;s documentation), a dynamic Makefile and some instructions. To execute the test suite, type <code>make test</code>. New tests can simply be placed into the directory and will be picked up by the Makefile and built automatically. I use the convention to name test files after the classes being tested. All .cpp/.h-files starting with <code>test_</code> belong to the test environment.</p>
<p>If you have never worked with CppUnit before, I recommend <a href="http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html">The CppUnit Cookbook</a>, which provides an excellent introduction.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/unmaintainable.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/unmaintainable.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/unmaintainable.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/unmaintainable.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/unmaintainable.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/unmaintainable.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/unmaintainable.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/unmaintainable.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/unmaintainable.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/unmaintainable.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/unmaintainable.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/unmaintainable.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unmaintainable.wordpress.com&blog=586265&post=5&subd=unmaintainable&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://unmaintainable.wordpress.com/2006/12/13/testing-c-apps-using-cppunit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/59c9677a3b9569af44561adab6c2a980?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mafr</media:title>
		</media:content>
	</item>
	</channel>
</rss>