<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Faye Williams | C &amp; C++ Programming Blog</title>
	
	<link>http://www.fayewilliams.com</link>
	<description>Simple Programming : A Girl Geek Guide to C and C++</description>
	<lastBuildDate>Thu, 19 Jan 2012 09:38:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/fayewilliams" /><feedburner:info uri="fayewilliams" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>fayewilliams</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><item>
		<title>Offline</title>
		<link>http://www.fayewilliams.com/2012/01/06/offline/</link>
		<comments>http://www.fayewilliams.com/2012/01/06/offline/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 12:08:19 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[Non-tech stuff]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1522</guid>
		<description><![CDATA[I&#8217;m taking a break in 2012 for a few months (maybe less, maybe more!). Two boys aged 2 and 4 months fill my days to capacity at the moment. Keep on coding!]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m taking a break in 2012 for a few months (maybe less, maybe more!).</p>
<p>Two boys aged 2 and 4 months fill my days to capacity at the moment.</p>
<p>Keep on coding!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=2LE0AZ3nY_M:JBQPNdsnLCo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=2LE0AZ3nY_M:JBQPNdsnLCo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=2LE0AZ3nY_M:JBQPNdsnLCo:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=2LE0AZ3nY_M:JBQPNdsnLCo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=2LE0AZ3nY_M:JBQPNdsnLCo:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=2LE0AZ3nY_M:JBQPNdsnLCo:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=2LE0AZ3nY_M:JBQPNdsnLCo:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=2LE0AZ3nY_M:JBQPNdsnLCo:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=2LE0AZ3nY_M:JBQPNdsnLCo:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2012/01/06/offline/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Programming | Unions in C</title>
		<link>http://www.fayewilliams.com/2011/12/14/simple-programming-unions-in-c/</link>
		<comments>http://www.fayewilliams.com/2011/12/14/simple-programming-unions-in-c/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 11:22:10 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[C Programming]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1511</guid>
		<description><![CDATA[Unions &#8211; ew. As primarily a C++ programmer, I used to shy away from code that contained this kind of black magic. However, unions are actually very cool things and are really not that scary or weird. A union is basically a variable. But instead of being an int, or a float, or a double, [...]]]></description>
			<content:encoded><![CDATA[<p>Unions &#8211; ew.</p>
<p>As primarily a C++ programmer, I used to shy away from code that contained this kind of black magic. However, unions are actually very cool things and are really not that scary or weird.</p>
<p>A union is basically a variable. But instead of being an int, or a float, or a double, a union can actually be any one of these things at a given point in time.</p>
<h3>What? That doesn&#8217;t make any sense!</h3>
<p>Wait &#8211; let me illustrate with an example.</p>
<p>The code below contains a union declaration at the top. Unions are declared just like structs, but instead of containing ALL the variables listed, <strong>a union only contains enough space for the largest one</strong>.</p>
<p>So in my example, where the union contains an int and a char &#8211; how big do you think will it be?</p>
<p>That&#8217;s right, it will be a single variable the same size as an int, because an int is larger than a char.</p>
<p>Storing and accessing union data is done just like it is with a struct:</p>
<pre class="brush:cpp">#include &lt;stdio.h&gt;

int main()
{
    union ascii
    {
        int number;
        char symbol;
    } a;

    /* Incoming data - could come from a variety of sources: */

    a.symbol = 'x';
    a.number = 120; 

    /* output last type in (int) */
    printf("Variable a is set to %d\n", a.number);

    return 0;
}</pre>
<h3></h3>
<h3>Okaaay&#8230; but how do you know what it is when it can be multiple things?</h3>
<p>Good question. You can&#8217;t actually tell what is inside a union, but the rule is that whatever you put in last, will be what you get out, and it is YOUR responsibility to remember what it was.</p>
<p>The best way to do this is with a variable that you update to contain the correct type (use an enum of INT, FLOAT, etc.)</p>
<p>If you access a union using a type that isn&#8217;t the same as the one last stored, what you get back is undefined.</p>
<p>Unions are really useful when you want to save storage space (especially good in embedded programming), or if you are receiving data that may arrive in different types (but which is essentially being used for the same purpose).</p>
<p>See? Not so scary after all.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=aUxWyVIFZ6o:7ny1HZo4Ofg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=aUxWyVIFZ6o:7ny1HZo4Ofg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=aUxWyVIFZ6o:7ny1HZo4Ofg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=aUxWyVIFZ6o:7ny1HZo4Ofg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=aUxWyVIFZ6o:7ny1HZo4Ofg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=aUxWyVIFZ6o:7ny1HZo4Ofg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=aUxWyVIFZ6o:7ny1HZo4Ofg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=aUxWyVIFZ6o:7ny1HZo4Ofg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=aUxWyVIFZ6o:7ny1HZo4Ofg:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/12/14/simple-programming-unions-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Programming | Boost Compiled Libraries in 3 Minutes</title>
		<link>http://www.fayewilliams.com/2011/12/07/simple-programming-boost-compiled-libraries-in-3-minutes/</link>
		<comments>http://www.fayewilliams.com/2011/12/07/simple-programming-boost-compiled-libraries-in-3-minutes/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 10:26:26 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[C++ Programming]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1473</guid>
		<description><![CDATA[Boost is mostly made of just header files, as we saw last week, which means you include them in your source, add the correct namespace and you&#8217;re good to go. However, there are a handful of compiled libraries too, so let&#8217;s take a very quick look at how we can use these. If you haven&#8217;t installed [...]]]></description>
			<content:encoded><![CDATA[<p>Boost is mostly made of just header files, as we saw last week, which means you include them in your source, add the correct namespace and you&#8217;re good to go.</p>
<p>However, there are a handful of compiled libraries too, so let&#8217;s take a very quick look at how we can use these. If you haven&#8217;t installed Boost, take a look at <a title="Simple Programming | Get Started With Boost in 3 Minutes" href="http://www.fayewilliams.com/2011/11/30/simple-programming-get-started-with-boost-in-3-minutes/">Boost in 3 Minutes</a> to get you up and running.</p>
<p>&nbsp;</p>
<p><strong>1) Include the header of the library you are interested in using</strong></p>
<p>I&#8217;m going to use the filesystem library:</p>
<pre class="brush:cpp">#include &lt;boost/filesystem.hpp&gt;</pre>
<p>Time taken: <span style="color: #ff0000;">30 seconds</span></p>
<p>&nbsp;</p>
<p><strong>2) Write some code using this library</strong></p>
<p>Don&#8217;t forget to qualify the methods with a namespace (else it will not compile!)</p>
<pre class="brush:cpp">#include &lt;boost/filesystem.hpp&gt;
#include &lt;iostream&gt;

int main()
{
    //don't forget this bit!
    using namespace boost::filesystem;

    path p("/var/log/dmesg");

    if (exists(p))
    {
        std::cout &lt;&lt; p &lt;&lt; " size is "
            &lt;&lt; file_size(p) &lt;&lt; std::endl;
    }

    return 0;
}</pre>
<p>Time taken: <span style="color: #ff0000;">2 minutes</span></p>
<p>&nbsp;</p>
<p><strong>3) Compile your code with the boost libraries</strong></p>
<p>Now, instead of just compiling our file, we have to include the filesystem library and let the compiler know where it lives:</p>
<pre>g++ boost.cpp -I/usr/include/boost -lboost_filesystem -lboost_system</pre>
<p>To clarify, the line above is made up of the following things:</p>
<p>&nbsp;</p>
<p>Firstly I&#8217;ve passed the source code file to the compiler.</p>
<p>Secondly I&#8217;ve added the path to boost using the <span style="font-family: Courier;">-I</span> option.</p>
<p>Thirdly I&#8217;ve added the libraries I want to link to using <span style="font-family: Courier;">-l</span>.</p>
<p>&nbsp;</p>
<p>In this case there are two libraries that I need to include, as the filesystem binary depends on the system binary. You will get a linker error if there are any dependencies when you include your initial library. This will tell you what else you might need to add.</p>
<p>Time taken: <span style="color: #ff0000;">30 seconds</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>The output of the little program above should be something like:</p>
<pre>/var/log/dmesg size is 61318</pre>
<p>And there you have it &#8211; boost compiled libraries in under 3 minutes :-)</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=4cNkmRgnTXk:P3LU-oPP-Cc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=4cNkmRgnTXk:P3LU-oPP-Cc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=4cNkmRgnTXk:P3LU-oPP-Cc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=4cNkmRgnTXk:P3LU-oPP-Cc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=4cNkmRgnTXk:P3LU-oPP-Cc:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=4cNkmRgnTXk:P3LU-oPP-Cc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=4cNkmRgnTXk:P3LU-oPP-Cc:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=4cNkmRgnTXk:P3LU-oPP-Cc:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=4cNkmRgnTXk:P3LU-oPP-Cc:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/12/07/simple-programming-boost-compiled-libraries-in-3-minutes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Programming | Get Started With Boost in 3 Minutes</title>
		<link>http://www.fayewilliams.com/2011/11/30/simple-programming-get-started-with-boost-in-3-minutes/</link>
		<comments>http://www.fayewilliams.com/2011/11/30/simple-programming-get-started-with-boost-in-3-minutes/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 11:28:47 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[C++ Programming]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1433</guid>
		<description><![CDATA[Boost is simply a collection of C++ libraries that provide lots of fun things to do in C++ without you having to write the code yourself. For example, Boost contains libraries to help you with mathematical calculations, regular expressions, smart pointers and even python integration. Parts of Boost have already been integrated into C++11, but [...]]]></description>
			<content:encoded><![CDATA[<p>Boost is simply a collection of C++ libraries that provide lots of fun things to do in C++ without you having to write the code yourself.</p>
<p>For example, Boost contains libraries to help you with mathematical calculations, regular expressions, smart pointers and even python integration.</p>
<p>Parts of Boost have already been integrated into C++11, but if you (or your organisation) is not quite ready for C++11 yet, or you want to play with the other Boost libraries, you can get to grips with it really easily in just 3 minutes.</p>
<p>This guide covers the header only libraries (i.e. most of them). I&#8217;ll take a look at the compiled libraries next week.</p>
<p>&nbsp;</p>
<p><strong>1) Install Boost</strong></p>
<p>As root/sudo type:</p>
<pre>yum install boost-devel</pre>
<p>You must install &#8216;boost-devel&#8217;, rather than just &#8216;boost&#8217;, or you will only get the compiled libraries and not the headers. Since boost is mostly header-only libraries, this won&#8217;t be much use!</p>
<p>Time taken: <span style="color: #ff0000;">30 seconds</span></p>
<p>&nbsp;</p>
<p><strong>2) Write yourself a program and Boost it!</strong></p>
<p>In order to use the boost methods, you must include the namespace qualifier.</p>
<p>If you don&#8217;t, your compiler will tell you that the methods are undefined and you may end up on a wild goose chase trying to ensure that boost is installed correctly and that the header files are being found. Stop right there!</p>
<p>You can either include it in one line (as I have done here), or you can individually qualify each method you use, e.g. with boost::gregorian::.</p>
<p>&nbsp;</p>
<pre class="brush:cpp">//include the boost header file for the library you want to use
#include &lt;boost/date_time/gregorian/gregorian.hpp&gt;
#include &lt;iostream&gt;

int main()
{
    //don't miss this out or it won't compile
    using namespace boost::gregorian;

    //construct a date object
    date xmas(2011,Dec,25);

    //print the day of the week
    std::cout &lt;&lt; "Christmas 2011 falls on ";
    std::cout  &lt;&lt; xmas.day_of_week() &lt;&lt; std::endl;

    //add a (leap) year
    date_duration leapyr(366);
    xmas += leapyr;

    //print day of the week next year
    std::cout &lt;&lt; "Christmas 2012 falls on ";
    std::cout &lt;&lt; xmas.day_of_week() &lt;&lt; std::endl;

    return 0;
}</pre>
<p>Time taken: <span class="Apple-style-span" style="color: #ff0000;">2 minutes</span></p>
<p>&nbsp;</p>
<p><strong>3) Compile and run your program</strong></p>
<p><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">g++ boost.cpp</span></p>
<p>The output should be:</p>
<pre>Christmas 2011 falls on Sun
Christmas 2012 falls on Tue</pre>
<p>Then sit back and feel very pleased with yourself.</p>
<p>Time taken: <span style="color: #ff0000;">30 seconds</span></p>
<p>&nbsp;</p>
<p>If you thought that was fab and want to learn what else you can do, full documentation is available <a title="boost documentation" href="http://www.boost.org/doc/">here</a>.</p>
<p>Can&#8217;t argue with that!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=n0F-NEtHr_Q:jhMWSQNNcNk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=n0F-NEtHr_Q:jhMWSQNNcNk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=n0F-NEtHr_Q:jhMWSQNNcNk:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=n0F-NEtHr_Q:jhMWSQNNcNk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=n0F-NEtHr_Q:jhMWSQNNcNk:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=n0F-NEtHr_Q:jhMWSQNNcNk:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=n0F-NEtHr_Q:jhMWSQNNcNk:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=n0F-NEtHr_Q:jhMWSQNNcNk:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=n0F-NEtHr_Q:jhMWSQNNcNk:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/11/30/simple-programming-get-started-with-boost-in-3-minutes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Programming | Find Files Installed by Yum</title>
		<link>http://www.fayewilliams.com/2011/11/23/simple-programming-find-files-installed-by-yum/</link>
		<comments>http://www.fayewilliams.com/2011/11/23/simple-programming-find-files-installed-by-yum/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 11:43:12 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1417</guid>
		<description><![CDATA[Yum is great isn&#8217;t it? All you do is tell it the name of the program or library and off it goes, installing files all over your computer, ready for you to use at the drop of a hat. However, sometimes it&#8217;s nice to know a little bit more about what yum is up to. [...]]]></description>
			<content:encoded><![CDATA[<p>Yum is great isn&#8217;t it? All you do is tell it the name of the program or library and off it goes, installing files all over your computer, ready for you to use at the drop of a hat.</p>
<p>However, sometimes it&#8217;s nice to know a little bit more about what yum is up to. Have you ever found yourself hunting all over your hard drive, trying to find exactly where yum has installed something?</p>
<p>Me too.</p>
<p>Well, if you do ever need to quiz yum, the following commands will allow you to gather all the knowledge you need on what your latest install has done.</p>
<p>As with pretty much everything on Linux, yum keeps a record of what it does. This record lives in:</p>
<p>/var/log/yum.log</p>
<p>So you could take a look in this file and see what packages were installed on what date.</p>
<p>But wait!</p>
<p>Yum itself has a history command, so you can also see this information in a much prettier way by typing:</p>
<pre>yum history</pre>
<p><img class="alignnone size-full wp-image-1418" title="yumhistory" src="http://www.fayewilliams.com/wp-content/uploads/2011/11/yumhistory.png" alt="yum history" width="500" height="348" /></p>
<p>And then using the ID in the left column to get more information about each record:</p>
<pre>yum history info 24</pre>
<p>Finally, and most importantly, what if you need to know where yum has installed all the files from your latest update?</p>
<p>Well, yum uses rpm for installation, so you can use it to query the package, like this:</p>
<pre>rpm -ql eclipse-cdt</pre>
<p>Which will give you a list of the full path to every file that was installed onto your computer for the package in question (I&#8217;ve used eclipse-cdt, but obviously you can ask it about anything you&#8217;ve installed).</p>
<p>Handy!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=GJJQFfo4fpc:4QB_UmQI51M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=GJJQFfo4fpc:4QB_UmQI51M:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=GJJQFfo4fpc:4QB_UmQI51M:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=GJJQFfo4fpc:4QB_UmQI51M:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=GJJQFfo4fpc:4QB_UmQI51M:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=GJJQFfo4fpc:4QB_UmQI51M:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=GJJQFfo4fpc:4QB_UmQI51M:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=GJJQFfo4fpc:4QB_UmQI51M:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=GJJQFfo4fpc:4QB_UmQI51M:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/11/23/simple-programming-find-files-installed-by-yum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Doh!</title>
		<link>http://www.fayewilliams.com/2011/11/18/doh/</link>
		<comments>http://www.fayewilliams.com/2011/11/18/doh/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 12:20:19 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1359</guid>
		<description><![CDATA[Well folks, as mentioned in How To Learn From Your Programming Mistakes, here it is. The story that still makes me shudder when I think about it. When it comes to all time, super-duper, public cock-ups, this one is almost the programming equivalent of Ratners. Strictly speaking I wasn&#8217;t actually trying to program anything when [...]]]></description>
			<content:encoded><![CDATA[<p>Well folks, as mentioned in <a title="How to Learn From Your Programming Mistakes (in 5 Easy Steps)" href="http://www.fayewilliams.com/2011/11/17/how-to-learn-from-your-programming-mistakes-in-5-easy-steps/">How To Learn From Your Programming Mistakes</a>, here it is.</p>
<p>The story that still makes me shudder when I think about it. When it comes to all time, super-duper, public cock-ups, this one is almost the programming equivalent of Ratners.</p>
<p>Strictly speaking I wasn&#8217;t actually trying to program anything when it happened. I was trying to&#8230; Oh let&#8217;s just start from the beginning shall we?</p>
<h3>The danger of embedded systems</h3>
<p>Not long out of university, I was working on an embedded linux platform. The embedded OS was accessed via telnet from Linux desktop machines. At any one time your terminal window could be logged into your desktop, or into the embedded Linux platform (can you see where this is going?).</p>
<p>I was asked to help out a colleague who was having trouble getting his embedded system up and running for testing.</p>
<p>He pointed me to his machine in the lab and then disappeared, leaving me to it.</p>
<p>Straight away I could see that the embedded board he was using was in a mess. Files were missing or corrupt and it was basically unrecoverable.</p>
<p>There were several terminal windows open at the same time, so I was switching between them to check build version, firmware version and to access my own machine remotely. I downloaded the latest build from my machine and logged into the system to reinstall it.</p>
<p>At least, I thought I did.</p>
<p>I typed:</p>
<pre>rm -rf /</pre>
<p>to delete everything, so I could start the install from scratch<sup>[1]</sup>.</p>
<p>But as I pressed the return key, I realised I was using a terminal window <em>on his desktop machine</em>.</p>
<p>And I was logged in as root.</p>
<p>If I had to describe that moment using a movie scene, it would be this one<sup>[2]</sup>:</p>
<p>&nbsp;</p>
<p><iframe src="http://www.youtube.com/embed/svEPWBxpYjo" frameborder="0" width="560" height="315"></iframe></p>
<p>&nbsp;</p>
<p>I totally destroyed his filesystem. He came to me with a broken embedded board and ended up with no test system and no desktop computer either.</p>
<h3>What did I do?</h3>
<p>After I&#8217;d finished reeling from the massive, MASSIVE mistake I&#8217;d just made, I gathered myself together, went to find my poor victim and confessed.</p>
<p>And apologized.</p>
<p>A lot.</p>
<h3>What happened?</h3>
<p>He very graciously took it in his stride (good, since there was bugger all he could do about it), and seemed happy enough once I promised I would immediately reinstall his operating system AND fix the test system.</p>
<p>I could have done it all in secret, and feigned ignorance if he had asked where his local files had gone.</p>
<p>But I owned up, and it wasn&#8217;t as bad as I thought it would be.</p>
<p>And now I can laugh about it (just).</p>
<p>And of course, I have NEVER made the same mistake again.</p>
<p>&nbsp;</p>
<p><em>[1] The command I used, for those of you that aren&#8217;t familiar with Linux, is the equivalent of selecting your hard drive and pressing the delete key.</em></p>
<p><em>[2] Jaws contains a classic example of the Dolly Zoom: that wonderfully abused effect where the background moves away while the actor appears to loom larger, usually accompanied by some tense string music.</em></p>
<p>&nbsp;</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=dzG4o2_7ek4:i8QikAW3_n4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=dzG4o2_7ek4:i8QikAW3_n4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=dzG4o2_7ek4:i8QikAW3_n4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=dzG4o2_7ek4:i8QikAW3_n4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=dzG4o2_7ek4:i8QikAW3_n4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=dzG4o2_7ek4:i8QikAW3_n4:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=dzG4o2_7ek4:i8QikAW3_n4:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=dzG4o2_7ek4:i8QikAW3_n4:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=dzG4o2_7ek4:i8QikAW3_n4:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/11/18/doh/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Learn From Your Programming Mistakes (in 5 Easy Steps)</title>
		<link>http://www.fayewilliams.com/2011/11/17/how-to-learn-from-your-programming-mistakes-in-5-easy-steps/</link>
		<comments>http://www.fayewilliams.com/2011/11/17/how-to-learn-from-your-programming-mistakes-in-5-easy-steps/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 08:40:00 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=948</guid>
		<description><![CDATA[Once upon a time, I made a very big mistake on a project at work. It was astonishingly stupid of me. I&#8217;ll share that wonderful event with you tomorrow (see how I&#8217;m craftily tempting you to come back and visit me again?). In the meantime, here&#8217;s my take on how you can learn from those [...]]]></description>
			<content:encoded><![CDATA[<p>Once upon a time, I made a very big mistake on a project at work. It was astonishingly stupid of me.</p>
<p>I&#8217;ll share that wonderful event with you <a title="Doh!" href="http://www.fayewilliams.com/2011/11/18/doh/">tomorrow</a> (see how I&#8217;m craftily tempting you to come back and visit me again?). In the meantime, here&#8217;s my take on how you can learn from those moments you&#8217;d rather forget&#8230;</p>
<h3>1. Acknowledge what you did wrong</h3>
<p>Even in this day and age of tools that record everything, automated comments, log files and digital history, there are still programmers out there who won&#8217;t admit when they have made a mistake. This varies from the vague &#8216;I don&#8217;t know how that happened,&#8217; to the more outrageous &#8216;It wasn&#8217;t me!&#8217;.</p>
<p>Programmers, more than any other professionals I know, are very protective of their work and <em>hate</em> to look like they don&#8217;t know what they are doing. Compounding this is the culture of schadenfreude in finding fault with peers, and the retelling of major errors over and over again, especially on team events and nights out: &#8220;hey dude, at least I&#8217;ve never checked in code with half an email to my wife pasted in the comment at the top, ha ha ha!&#8221;</p>
<p>These stories, that provide so much amusement to everyone listening (except the protagonist), are popular because they make us all feel that we aren&#8217;t the only ones that make mistakes. And you know what? That&#8217;s actually a good thing.</p>
<p>No one gets it right all the time, so why try and hide your errors? As embarrassing as they are, covering them up and trying to forget about them is counter-productive. Not only are you trying to fool others that you never get it wrong, you are also fooling yourself that you don&#8217;t need to improve, or take more care, or understand a tool better, or learn something new.</p>
<p>So next time you check in a source file and find you&#8217;ve accidentally left memory leaking all over the show, stand up and be proud to admit it. In fact, get in first and tell people what you&#8217;ve done. Throw your hands in the air and shout about it. Apologise to your manager for causing 15 crashes in the overnight test suite.</p>
<p>Be human and accept that the only errors computers make are the ones we tell them to make.</p>
<h3>2. Understand why it was wrong</h3>
<p>In some cases this is easy, but in others you need to really look at why you made the mistake in the first place. If you don&#8217;t really, truly understand exactly <strong>what</strong> the error is that you made, and <strong>why</strong> you made it, you can&#8217;t be sure that you won&#8217;t ever do it again.</p>
<p>Was it a lack of understanding in a particular area? Maybe you don&#8217;t fully appreciate the workings of the library you are using, or your knowledge of pointers is less than perfect, or you&#8217;ve never really worked with this particular language/algorithm/operating system.</p>
<p>Whatever it is, understand <strong>why</strong>. It could be as simple as the fact that you were rushing and trying to do too many things at once (focus, people &#8211; attention to detail is one of your biggest assets). Or it could be that you were making modifications to code using intuition rather than knowledge. (Oops. Guilty.)</p>
<p>Once you know exactly why you made a mistake, you are a very, very long way ahead of the person next to you that just sweeps it under the carpet and tries to forget about it.</p>
<h3>3. Commit to improving</h3>
<p>Here&#8217;s the hard bit. This is the step that won&#8217;t happen instantly and that won&#8217;t happen by itself either. You HAVE to make the effort to improve in the area in which you made the mistake.</p>
<p>Created a mind-boggling memory leak? Read up on <a title="Simple Programming | Dynamic Memory in C (malloc, calloc, realloc, free)" href="http://www.fayewilliams.com/2011/11/09/simple-programming-dynamic-memory-in-c-malloc-calloc-realloc-free/">malloc and free</a> (or <a title="Simple Programming | Dynamic Memory in C++ (new and delete)" href="http://www.fayewilliams.com/2011/11/16/simple-programming-dynamic-memory-in-c-new-and-delete/">new and delete</a>; pick your weapon and don&#8217;t get them mixed up). Got your iterators all over the place? Go back to basics with STL and learn what you were trying to do. Deleted an afternoon&#8217;s worth of code by accident? Get the manual out, google a tutorial, or read the help files and understand the tool you are working with.</p>
<p>Make the commitment to NEVER make that mistake again. But if you do, go back to step 1 ;-)</p>
<h3>4. Look back and laugh (eventually)</h3>
<p>It is very important to maintain a sense of humour about your mistakes. Going over things you have done wrong can destroy your confidence and leave you sitting in a corner, unwilling to contribute, failing to suggest ideas, refusing to raise your hand, and staying out of the debate.</p>
<p>Never let a big programming mistake, especially a publicly humiliating one (<a title="Doh!" href="http://www.fayewilliams.com/2011/11/18/doh/">my story tomorrow</a>!), take away your confidence or your desire for learning. Don&#8217;t give up.</p>
<p>If you admit your mistake, understand it, and improve your knowledge, then you will be able to forgive yourself.</p>
<p>And you might even end up as the one saying, &#8220;Hey dude, do you remember that time I checked in code with half an email to my wife pasted in the comment at the top, ha ha ha!&#8221;.</p>
<h3>5. Be gracious when someone else makes a mistake (very important)</h3>
<p>I&#8217;m not saying you can&#8217;t laugh at someone else&#8217;s coding misfortunes, or that you have to cuddle your colleagues and tell them it&#8217;s OK to mess up the overnight test suite.</p>
<p>What I am saying is don&#8217;t go overboard on the humiliation. We left a voodoo doll on the desk of a colleague that messed up a release build one evening (OK, that&#8217;s actually quite horrible). BUT we were nice about it. We rallied around and talked about the disasters we had caused, we supported his mistake by talking about our own.</p>
<p>Remember that you&#8217;re all in it together. Your team, however diverse and intelligent/incompetent they are, will ALL benefit if everyone approaches mistakes with the right mindset.</p>
<p><strong>Admit.</strong></p>
<p><strong>Understand.</strong></p>
<p><strong>Learn.</strong></p>
<p><strong>Laugh.</strong></p>
<p><strong>Join in.</strong></p>
<p>Easy, eh?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=pSj6jQt2Vpc:X8mKu1RMElE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=pSj6jQt2Vpc:X8mKu1RMElE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=pSj6jQt2Vpc:X8mKu1RMElE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=pSj6jQt2Vpc:X8mKu1RMElE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=pSj6jQt2Vpc:X8mKu1RMElE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=pSj6jQt2Vpc:X8mKu1RMElE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=pSj6jQt2Vpc:X8mKu1RMElE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=pSj6jQt2Vpc:X8mKu1RMElE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=pSj6jQt2Vpc:X8mKu1RMElE:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/11/17/how-to-learn-from-your-programming-mistakes-in-5-easy-steps/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Simple Programming | Dynamic Memory in C++ (new and delete)</title>
		<link>http://www.fayewilliams.com/2011/11/16/simple-programming-dynamic-memory-in-c-new-and-delete/</link>
		<comments>http://www.fayewilliams.com/2011/11/16/simple-programming-dynamic-memory-in-c-new-and-delete/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 09:54:14 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[C++ Programming]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1344</guid>
		<description><![CDATA[We looked at dynamic memory in C last week, so let&#8217;s compare it with dynamic memory in C++. Allocation In C++, if you want to allocate on the heap rather than the stack, you need to use the new operator. Just like the C equivalents, the new operator returns a void* pointer to the block [...]]]></description>
			<content:encoded><![CDATA[<p>We looked at <a title="Simple Programming | Dynamic Memory in C (malloc, calloc, realloc, free)" href="http://www.fayewilliams.com/2011/11/09/simple-programming-dynamic-memory-in-c-malloc-calloc-realloc-free/">dynamic memory in C</a> last week, so let&#8217;s compare it with dynamic memory in C++.</p>
<h3>Allocation</h3>
<p>In C++, if you want to allocate on the heap rather than the stack, you need to use the <em>new</em> operator.</p>
<p>Just like the C equivalents, the <em>new</em> operator returns a void* pointer to the block of memory that has been allocated.</p>
<p>There are two versions of <em>new</em>, the first returns a single item:</p>
<pre class="brush:cpp,gutter:false">int* x;
x = new int;</pre>
<p>The second, using square brackets, returns an array of items:</p>
<pre class="brush:cpp,gutter:false">int* x;
x = new int[16];</pre>
<h3>Checking for success</h3>
<p>C allocators return NULL and set errno when they fail, which you should explicitly check for.</p>
<p>In C++ however, an exception is thrown (<em>bad_alloc</em>). If you don&#8217;t have the code in place to catch the exception, your program will terminate.</p>
<p>You can force C++ to behave like C by passing the <em>nothrow</em> argument to <em>new</em>:</p>
<pre class="brush:cpp,gutter:false">int* x;
x = new(nothrow) int;</pre>
<p>Now <em>new</em> will behave like a C function and set x to NULL if it fails.</p>
<p>Of course, if you do this, you&#8217;ll remember check for a NULL value before using the pointer, won&#8217;t you?</p>
<h3>Clearing up</h3>
<p>Just like in C, you MUST remember to clear up after yourself when you&#8217;ve spent the afternoon happily declaring memory on the heap.</p>
<p>To do this, you need to call <em>delete</em>. There are two versions of <em>delete</em> that match the two versions of <em>new</em>:</p>
<pre class="brush:cpp,gutter:false">//for single objects
delete x;</pre>
<pre class="brush:cpp,gutter:false">//for arrays
delete[] x;</pre>
<h3>What if you use the wrong one?</h3>
<p>If you forget to use the brackets on an allocated array, you will only delete the first element. The others will remain inaccessible in memory, causing a memory leak. And you don&#8217;t want that to happen, do you?</p>
<p>If you use the bracketed version on a single object the behaviour is undefined. Which, if you&#8217;re unsure, means you can&#8217;t rely on the result to be anything you might expect :-)</p>
<h3>Can you mix and match between new, malloc, delete and free?</h3>
<p>No. Never do this!</p>
<p>First off, it&#8217;s just plain wrong to mix functionality from two separate languages (even though you can).</p>
<p>Second, <em>new</em> and <em>delete</em> are used in C++ because they call the constructor and destructor of an object respectively. The C equivalents, <em>malloc</em> and <em>free</em> do not do this.</p>
<p>So, if you create an object with <em>malloc</em>, any work done in the constructor will not happen.</p>
<p>Worse, if you <em>free</em> an object that was allocated with <em>new</em> (instead of using delete), the object&#8217;s destructor is not called. If your object uses memory dynamically and the destructor does the clearing up, then what will you have? Yes, a memory leak.</p>
<p>But that won&#8217;t happen to you now, because you&#8217;ve just read this, right? ;-)</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=b7mbdMWX-fU:1JJPwT5D5dg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=b7mbdMWX-fU:1JJPwT5D5dg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=b7mbdMWX-fU:1JJPwT5D5dg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=b7mbdMWX-fU:1JJPwT5D5dg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=b7mbdMWX-fU:1JJPwT5D5dg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=b7mbdMWX-fU:1JJPwT5D5dg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=b7mbdMWX-fU:1JJPwT5D5dg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=b7mbdMWX-fU:1JJPwT5D5dg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=b7mbdMWX-fU:1JJPwT5D5dg:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/11/16/simple-programming-dynamic-memory-in-c-new-and-delete/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple Programming | Dynamic Memory in C (malloc, calloc, realloc, free)</title>
		<link>http://www.fayewilliams.com/2011/11/09/simple-programming-dynamic-memory-in-c-malloc-calloc-realloc-free/</link>
		<comments>http://www.fayewilliams.com/2011/11/09/simple-programming-dynamic-memory-in-c-malloc-calloc-realloc-free/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 20:55:27 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[C Programming]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1300</guid>
		<description><![CDATA[Let&#8217;s take a look at the four methods that allow us to utilize dynamic memory in C. Dynamic memory just means we are using memory on the heap, instead of on the stack. Why would you want to use dynamic memory? You might want to create a variable or object that persists beyond the scope [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s take a look at the four methods that allow us to utilize dynamic memory in C. Dynamic memory just means we are using memory on the heap, instead of on the stack.</p>
<h3>Why would you want to use dynamic memory?</h3>
<p>You might want to create a variable or object that persists beyond the scope of the function it is created in (i.e. to share it between functions). The only way to do this is to create it dynamically, and then remember to deallocate it at a later point. Alternatively, you might not know the size of something you are using until runtime (e.g. reading in a file), in which case the heap is a better place to store your data. Why? Because the size of the stack is limited and it may be too small for unknown data processed at runtime.</p>
<h3>How do you dynamically allocate in C?</h3>
<p>There are three functions for memory allocation (plus one for deallocation). Each allocator returns a void* to the memory that has been allocated, which can be cast to the appropriate type.</p>
<p><strong>Let&#8217;s look at each in turn:</strong></p>
<p><em>1. malloc</em></p>
<p>This is the most commonly used method. Simply pass in how big you want your memory to be (in bytes), and you get a pointer to that memory back. The memory is uninitialized. If it fails it returns NULL.</p>
<p><em>2. calloc</em></p>
<p>Instead of passing in a size, you tell <em>calloc</em> how many of a certain type of variable you are going to use. E.g. 10 ints, or 16 structs. The memory is initialized to zeros. If it fails it returns NULL.</p>
<p><em>3. realloc</em></p>
<p>This method resizes an existing block of memory and you can make your existing memory allocation bigger or smaller. It frees the existing block and returns a void* to the new block. If you pass in zero, it effectively frees the memory in question. If it fails it returns NULL (see the comments in the code below for why you should pay careful attention to how you use realloc).</p>
<h3>And what do you do when you&#8217;re finished?</h3>
<p>You call <em>free</em>, nice and easy, and your memory is released (although it is not &#8220;deleted&#8221; as such &#8211; the data may exist until something else overwrites it, or part of it, or until the program ends).</p>
<p>Note that calls to <em>malloc</em>, <em>calloc</em> and <em>realloc</em> set <em>errno</em> if they fail. You can find out how to <a title="Simple Programming | Using errno" href="http://www.fayewilliams.com/2011/10/19/simple-programming-using-errno/">use errno here</a>!</p>
<h3>Can I see an example?</h3>
<p>Sure &#8211; here&#8217;s a little bit of code that uses all four methods so you can see them at work:</p>
<pre class="brush:cpp">#include &lt;stdlib.h&gt;

#define BIG_NUMBER 1024
#define SMALL_NUMBER 16

struct msg
{
	int code;
	char message[BIG_NUMBER];
};

int main(void)
{
	char* buffer;
	struct msg* messagelist;

	/* Allocate some memory from the heap */
	buffer = (char*)malloc(BIG_NUMBER);
	if (buffer != NULL)
	{
		/* I can use the memory safely */
	}

	/* Reduce the size of the memory */
	char* smallbuffer = (char*)realloc(buffer, SMALL_NUMBER);
	if (smallbuffer != NULL)
	{
		/* I can use the memory safely */
	}

	/*******************************************
	 * NOTE: Look carefully at the realloc call above.
	 * If the call to realloc had failed and I had assigned
	 * it to the original buffer like so:
	 *     buffer = (char*)realloc(buffer, SMALL_NUMBER);
	 * then my buffer would have been set to NULL and I would
	 * not only lose access to the data that was stored
	 * there, but I'd create a memory leak too!
	 *******************************************/

	/* Allocate some memory from the heap */
	messagelist = (struct msg*)calloc(SMALL_NUMBER, sizeof(struct msg));
	if (messagelist != NULL)
	{
		/* I can use the memory safely */
	}

	/* Remember to clear up after myself */
	free(smallbuffer);
	free(messagelist);

	/* NOTE: I DON'T need to free the 'buffer' variable */
	/* because realloc already did it for me :-) */

	return EXIT_SUCCESS;
}</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=uiVqPuO49kQ:pxr5YQ5p6Cw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=uiVqPuO49kQ:pxr5YQ5p6Cw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=uiVqPuO49kQ:pxr5YQ5p6Cw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=uiVqPuO49kQ:pxr5YQ5p6Cw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=uiVqPuO49kQ:pxr5YQ5p6Cw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=uiVqPuO49kQ:pxr5YQ5p6Cw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=uiVqPuO49kQ:pxr5YQ5p6Cw:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=uiVqPuO49kQ:pxr5YQ5p6Cw:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=uiVqPuO49kQ:pxr5YQ5p6Cw:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/11/09/simple-programming-dynamic-memory-in-c-malloc-calloc-realloc-free/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spotted in Game, Portsmouth</title>
		<link>http://www.fayewilliams.com/2011/11/06/spotted-in-game-portsmouth/</link>
		<comments>http://www.fayewilliams.com/2011/11/06/spotted-in-game-portsmouth/#comments</comments>
		<pubDate>Sun, 06 Nov 2011 19:19:23 +0000</pubDate>
		<dc:creator>Faye</dc:creator>
				<category><![CDATA[Frivolous Posts]]></category>

		<guid isPermaLink="false">http://www.fayewilliams.com/?p=1295</guid>
		<description><![CDATA[Windows failed to load because the kernel is missing, or corrupt. Doh.]]></description>
			<content:encoded><![CDATA[<p>Windows failed to load because the kernel is missing, or corrupt.</p>
<p>Doh.</p>
<p><img class="alignnone size-full wp-image-1296" title="windows fail" src="http://www.fayewilliams.com/wp-content/uploads/2011/11/IMG_0320.gif" alt="windows fail" width="500" height="349" /></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/fayewilliams?a=YvB_TkqbyWc:ePW2uPWg8vM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=YvB_TkqbyWc:ePW2uPWg8vM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=YvB_TkqbyWc:ePW2uPWg8vM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=YvB_TkqbyWc:ePW2uPWg8vM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=YvB_TkqbyWc:ePW2uPWg8vM:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=YvB_TkqbyWc:ePW2uPWg8vM:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=YvB_TkqbyWc:ePW2uPWg8vM:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/fayewilliams?i=YvB_TkqbyWc:ePW2uPWg8vM:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/fayewilliams?a=YvB_TkqbyWc:ePW2uPWg8vM:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/fayewilliams?d=dnMXMwOfBR0" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.fayewilliams.com/2011/11/06/spotted-in-game-portsmouth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

