<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Perilous Adventures of a Software Archaeologist &#187; c++</title>
	<atom:link href="http://chris.iluo.net/blog/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://chris.iluo.net/blog</link>
	<description>As endured by Chris Pilkington</description>
	<lastBuildDate>Wed, 25 Aug 2010 22:40:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>malloc double free/non- aligned pointer being freed set a breakpoint in malloc_error_break to debug</title>
		<link>http://chris.iluo.net/blog/2010/01/10/malloc-double-freenon-aligned-pointer-being-freed-set-a-breakpoint-in-malloc_error_break-to-debug/</link>
		<comments>http://chris.iluo.net/blog/2010/01/10/malloc-double-freenon-aligned-pointer-being-freed-set-a-breakpoint-in-malloc_error_break-to-debug/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 23:00:22 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/?p=227</guid>
		<description><![CDATA[malloc: *** error for object 0x3874a0: double free ***set a breakpoint in malloc_error_break to debug malloc: *** error for object 0x18a138: Non- aligned pointer being freed *** set a breakpoint in malloc_error_break to debug So basically something in your code &#8230; <a href="http://chris.iluo.net/blog/2010/01/10/malloc-double-freenon-aligned-pointer-being-freed-set-a-breakpoint-in-malloc_error_break-to-debug/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<pre>
malloc: *** error for object 0x3874a0: double free
***set a breakpoint in malloc_error_break to debug

malloc: *** error for object 0x18a138: Non- aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
</pre>
<p>So basically something in your code is screwing around with memory.</p>
<p>Either releasing something that has already been released:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> x<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">delete</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> x<span style="color: #008080;">;</span>
<span style="color: #0000dd;">delete</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> x<span style="color: #008080;">;</span></pre></div></div>

<p>Or releasing something that is not pointing to the start of an allocated block of memory:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">int</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
x<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">delete</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> x<span style="color: #008080;">;</span></pre></div></div>

<p>The error message isn&#8217;t very clear if you have no experience with <a href="http://en.wikipedia.org/wiki/GNU_Debugger">GDB</a>.  GDB is a debugger for your binaries.  It allows you to set break points at the start of a function and any time that function is called your application will pause and allow you to debug in GDB.  We can then get valuable information back by executing commands to get the backtrace, registers state and disassembly.  The advantage of using GDB over <a href="http://en.wikipedia.org/wiki/Xcode">Xcode</a>/<a href="http://en.wikipedia.org/wiki/KDevelop">KDevelop</a> is being able to break into any function, not just functions in your source code.  Anyway, this is how I got the backtrace to find out where in my sourcecode I was making a mistake:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">gdb</span>
<span style="color: #c20cb9; font-weight: bold;">file</span> myapplication
<span style="color: #7a0874; font-weight: bold;">break</span> malloc_error_break
run
backtrace</pre></div></div>

<p>Now whenever a double free or non- aligned pointer is freed it will break into gdb and we can type in &#8220;backtrace&#8221; and work out what our code did to trigger this.</p>
]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2010/01/10/malloc-double-freenon-aligned-pointer-being-freed-set-a-breakpoint-in-malloc_error_break-to-debug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When Good Customers Go Bad</title>
		<link>http://chris.iluo.net/blog/2009/09/26/when-good-customers-go-bad/</link>
		<comments>http://chris.iluo.net/blog/2009/09/26/when-good-customers-go-bad/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 22:36:47 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[oh noes]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/?p=218</guid>
		<description><![CDATA[class Customer; class CustomerEx; // Added 20.03.2006 class CustomerEx2; // Added 05.10.2006 class CustomerEx2B; // Added 13.07.2008]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Customer<span style="color: #008080;">;</span>
<span style="color: #0000ff;">class</span> CustomerEx<span style="color: #008080;">;</span> <span style="color: #666666;">// Added 20.03.2006</span>
<span style="color: #0000ff;">class</span> CustomerEx2<span style="color: #008080;">;</span> <span style="color: #666666;">// Added 05.10.2006</span>
<span style="color: #0000ff;">class</span> CustomerEx2B<span style="color: #008080;">;</span> <span style="color: #666666;">// Added 13.07.2008</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2009/09/26/when-good-customers-go-bad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>libxdgmm</title>
		<link>http://chris.iluo.net/blog/2009/07/22/libxdgmm/</link>
		<comments>http://chris.iluo.net/blog/2009/07/22/libxdgmm/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 22:46:05 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[libxdgmm]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[xdg]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/?p=150</guid>
		<description><![CDATA[I think the Portland Project from freedesktop.org, is a great idea and everyone should be supporting it in their applications. I&#8217;ve just created a very small C++ wrapper (libxdgmm) for accessing XDG more easily. To use it, you need libxdgmm.h &#8230; <a href="http://chris.iluo.net/blog/2009/07/22/libxdgmm/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I think the <a href="http://en.wikipedia.org/wiki/Portland_Project">Portland Project</a> from <a href="http://www.freedesktop.org/">freedesktop.org</a>, is a great idea and<br />
everyone should be <a href="http://ploum.frimouvy.org/?207-modify-your-application-to-use-xdg-folders">supporting it in their applications</a>.</p>
<p>I&#8217;ve just created a very small C++ wrapper (libxdgmm) for accessing <a href="http://portland.freedesktop.org/wiki/">XDG</a> more easily.  To use it, you need <a href="http://breathe.git.sourceforge.net/git/gitweb.cgi?p=breathe/breathe;a=blob;f=include/libxdgmm/libxdgmm.h;hb=HEAD">libxdgmm.h</a> and <a href="http://breathe.git.sourceforge.net/git/gitweb.cgi?p=breathe/breathe;a=blob;f=src/libxdgmm/libxdgmm.cpp;hb=HEAD">libxdgmm.cpp</a>.  Just add these to your project and then use them like so:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;libxdgmm/libxdg.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> argv<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>xdg<span style="color: #008080;">::</span><span style="color: #007788;">IsInstalled</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;XDG is not installed&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> data<span style="color: #008080;">;</span>
    xdg<span style="color: #008080;">::</span><span style="color: #007788;">GetDataHome</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;data=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000080;">&lt;&lt;</span>data<span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
&nbsp;
    std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> config<span style="color: #008080;">;</span>
    xdg<span style="color: #008080;">::</span><span style="color: #007788;">GetConfigHome</span><span style="color: #008000;">&#40;</span>config<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;config=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000080;">&lt;&lt;</span>config<span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Obviously these have to exist to work.  You can translate the error code returned by calling xdg::GetOpenErrorString(int result);</span>
    xdg<span style="color: #008080;">::</span><span style="color: #007788;">OpenFile</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;/home/chris/dev/cMd3Loader.cpp&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    xdg<span style="color: #008080;">::</span><span style="color: #007788;">OpenFolder</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;/home/chris/&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    xdg<span style="color: #008080;">::</span><span style="color: #007788;">OpenURL</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;http://chris.iluo.net&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">EXIT_SUCCESS</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I still have to wrap some of the other functionality, such as XDG_DESKTOP_DIR, XDG_DOCUMENTS_DIR, XDG_MUSIC_DIR, desktop-file-utils, xdg-desktop-menu and xdg-desktop-icon etc.  I will wrap these as I need them (Or at special request).  I don&#8217;t think I will be supporting xdg-screensaver or xdg-mime as I don&#8217;t have a use for them right now.</p>
]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2009/07/22/libxdgmm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++0x compiling with gcc</title>
		<link>http://chris.iluo.net/blog/2009/07/20/c0x-compiling-with-gcc/</link>
		<comments>http://chris.iluo.net/blog/2009/07/20/c0x-compiling-with-gcc/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 23:30:55 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[c++0x]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/?p=152</guid>
		<description><![CDATA[Gcc has some early support for C++0x. You can enable it with -std=c++0x (Note: You can already use Boost without enabling this flag). Enabling this flag broke boost::filesystem for me, but this has already been reported and fixed: boost/filesystem/operations.hpp 661 &#8230; <a href="http://chris.iluo.net/blog/2009/07/20/c0x-compiling-with-gcc/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Gcc has some early support for C++0x.  You can enable it with -std=c++0x (Note: You can already use <a href="http://www.boost.org/">Boost</a> without enabling this flag).  Enabling this flag broke boost::filesystem for me, but this has already been <a href="https://svn.boost.org/trac/boost/ticket/3008">reported</a> and <a href="https://svn.boost.org/trac/boost/attachment/ticket/3008/fs_gcc44.patch">fixed</a>:</p>
<p>boost/filesystem/operations.hpp</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">661</span>	<span style="color: #0000dd;">661</span>	    <span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">bool</span> is_empty<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> path <span style="color: #000040;">&amp;</span> ph <span style="color: #008000;">&#41;</span> 
<span style="color: #0000dd;">662</span>	 	<span style="color: #000040;">-</span>      <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> is_empty<span style="color: #000080;">&lt;</span>path<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> ph <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span> 
 	<span style="color: #0000dd;">662</span>	<span style="color: #000040;">+</span>      <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">filesystem</span><span style="color: #008080;">::</span><span style="color: #007788;">is_empty</span><span style="color: #000080;">&lt;</span>path<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> ph <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span> 
<span style="color: #0000dd;">663</span>	<span style="color: #0000dd;">663</span>	    <span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">bool</span> is_empty<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> wpath <span style="color: #000040;">&amp;</span> ph <span style="color: #008000;">&#41;</span> 
<span style="color: #0000dd;">664</span>	 	<span style="color: #000040;">-</span>      <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> is_empty<span style="color: #000080;">&lt;</span>wpath<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> ph <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span> 
 	<span style="color: #0000dd;">664</span>	<span style="color: #000040;">+</span>      <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">filesystem</span><span style="color: #008080;">::</span><span style="color: #007788;">is_empty</span><span style="color: #000080;">&lt;</span>wpath<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> ph <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span></pre></div></div>

<p>The full patch is much longer, but this seemed to be all I needed to get it to work (Note: You will need to <em>su</em> and then <em>gedit /usr/include/boost/filesystem/operations.hpp</em>).</p>
<p>Anyway, you will want to test it out:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">enum</span> A
<span style="color: #008000;">&#123;</span>
  A_1,
  A_2
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">enum</span> B
<span style="color: #008000;">&#123;</span>
  B_1,
  B_2
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> Test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  B b <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
  b <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
  b <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
  b <span style="color: #000080;">=</span> A_1<span style="color: #008080;">;</span>
  b <span style="color: #000080;">=</span> A<span style="color: #008080;">::</span><span style="color: #007788;">A_1</span><span style="color: #008080;">;</span>
  b <span style="color: #000080;">=</span> B_1<span style="color: #008080;">;</span>
  b <span style="color: #000080;">=</span> B<span style="color: #008080;">::</span><span style="color: #007788;">B_1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The only lines of Test that compile are &#8220;b = B_1;&#8221; and &#8220;b = B::B_1;&#8221;.  Under C++98/C++03 this would 100% compile, which could easily create bugs where the code compiles but may not be what is intended.  We can also get slightly tighter restrictions again, by defining &#8220;enum B&#8221; as &#8220;enum class B&#8221;.  This restricts the correct lines of Test to &#8220;b = B::B_1&#8243;, meaning the enum must be fully qualified which should lead to less ambiguous code.  In a real situation you will also want to actually initialise b to a decent value initially as well: &#8220;B b = B::B_1&#8243; for example.</p>
<p>Anyway, this is a pretty trivial example, just a tidy up of the language really, you will want to <a href="http://gcc.gnu.org/projects/cxx0x.html">delve deeper</a>.  Variadic templates, Initializer lists, Lambda expressions and closures, New character types, Unicode string literals, Raw string literals and Universal character name literals look good.  Extern templates, Inheriting constructors, nullptr, __func__, C99 preprocessor, long long, Extended integral types can&#8217;t get here soon enough.  </p>
]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2009/07/20/c0x-compiling-with-gcc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AHGotoPage Doesn&#8217;t Work Argh!</title>
		<link>http://chris.iluo.net/blog/2009/06/04/ahgotopage-doesnt-work-argh/</link>
		<comments>http://chris.iluo.net/blog/2009/06/04/ahgotopage-doesnt-work-argh/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 13:14:28 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[carbon]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/2009/06/04/ahgotopage-doesnt-work-argh/</guid>
		<description><![CDATA[It looks like AHGotoPage is broken with Mac OS X 10.5.7 and later (It was also broken on 10.3.9 and earlier). Probably the easiest way around this for the moment is to open your help documentation in a web browser &#8230; <a href="http://chris.iluo.net/blog/2009/06/04/ahgotopage-doesnt-work-argh/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It looks like <a href="http://developer.apple.com/documentation/Carbon/Reference/Apple_Help/Reference/reference.html#//apple_ref/doc/uid/TP30000169-CH1g-F16335">AHGotoPage</a> is broken with Mac OS X 10.5.7 and later (It was also broken on 10.3.9 and earlier).  Probably the easiest way around this for the moment is to open your help documentation in a web browser using <a href="http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/Reference/reference.html#//apple_ref/c/func/LSOpenCFURLRef">LSOpenCFURLRef</a> instead.  Hope this helps someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2009/06/04/ahgotopage-doesnt-work-argh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yearly Update :)</title>
		<link>http://chris.iluo.net/blog/2009/02/25/yearly-update/</link>
		<comments>http://chris.iluo.net/blog/2009/02/25/yearly-update/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 22:17:01 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[gamedev]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/?p=121</guid>
		<description><![CDATA[It&#8217;s been a while, I have like 20 draft entries in WordPress ranging from 1 paragraph comments up to 10 paragraph full on entries that still need that final once over and edit before going live. Actually it might be &#8230; <a href="http://chris.iluo.net/blog/2009/02/25/yearly-update/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="attachment_78" class="wp-caption alignright" style="width: 151px"><a href="http://chris.iluo.net/blog/wp-content/uploads/2009/02/sudoku090225.png"><img src="http://chris.iluo.net/blog/wp-content/uploads/2009/02/sudoku090225.png" alt="Sudoku Work in Progress" title="sudoku090225" width="141" height="111" class="size-full wp-image-78" /></a><p class="wp-caption-text">Skysytem background, testing wireframe grid, TombRaider MD3 model, testing material boxes, particle systems, frames per second messages, pegs, shocks, all the necessary elements of a Sudoku game.</p></div>
<p>It&#8217;s been a while, I have like 20 draft entries in <a href="http://www.wordpress.org/">WordPress</a> ranging from 1 paragraph comments up to 10 paragraph full on entries that still need that final once over and edit before going live.  Actually it might be cool if there was (There probably is) a plugin so that people could optionally go to a page on my blog where they can see everything that hasn&#8217;t been published yet, like tagged with &#8220;draft&#8221; or something and comment on which ones I should flesh out and which ones I should ditch before they are even finished.  </p>
<p>Anyway, so I have been working (Getting side tracked while working on) my <a href="http://en.wikipedia.org/wiki/Sudoku">Sudoku</a> game.  </p>
<p>Basically in Sudoku mode you get to select a number from the &#8220;palette&#8221; at the top and then click on all the places you want it on the board.  The solver I have coded up can solve about 80% of Sudoku boards with &#8220;human solvable&#8221; methods and the remaining 20% can be solved with a combination of human solvable and then resorting to brute force for anything it can&#8217;t find.  A valid board should not need brute forcing which means that I need to implement more rules for my human solving methods first.  I should be doing something like this:</p>
<p><code>if (solve_human_methods()) ... solved<br />
else ... this board is invalid as it could not possibly be solved by a human without resorting to brute force</code></p>
<p>However I haven&#8217;t implemented all human methods of solving yet, only about 4 or 5 simple ones, actually probably less, there are about 2 or 3 real rules and then 4 or 5 extrapolated rules that are just combinations of the first ones.</p>
<p>Anyway, in First Person mode there is flying around (No clip mode) as well as moving Lara Croft around (Optionally other MD3 models) with sort of appropriate animations based on velocity (But not facing direction yet).  One thing I have noticed is that half of the MD3 models I download have different file naming conventions, so at some stage I want to break out some Quake 3 action and see what file names it uses and use that as my standard.</p>
<p>Why all this other stuff in a Sudoku game?  Whichever game I am working at the time becomes my test bed application for whatever I feel like implementing when bored.  I really need to make a dedicated test bed that does nothing else but demonstrate stuff.  I&#8217;ve also split my game engine into <a href="http://breathe.git.sourceforge.net/git/gitweb.cgi?p=breathe/breathe;a=tree;f=include/spitfire;hb=HEAD">Spitfire</a> and <a href="http://breathe.git.sourceforge.net/git/gitweb.cgi?p=breathe/breathe;a=tree;f=include/breathe;hb=HEAD">Breathe</a> portions, which splits the library into two halves, the generic tools for any application (string, math, xml, md5 hashes etc.) and game specific features (<a href="http://www.opengl.org">OpenGL</a> rendering, audio, physics, MD3 animation, etc.) respectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2009/02/25/yearly-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>x86_64 Linux C/C++ Test</title>
		<link>http://chris.iluo.net/blog/2008/12/15/x86_64-linux-c-slash-c-plus-plus-test/</link>
		<comments>http://chris.iluo.net/blog/2008/12/15/x86_64-linux-c-slash-c-plus-plus-test/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 05:10:02 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[c++]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[x86_64]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/?p=36</guid>
		<description><![CDATA[I use: gcc cmake KDevelop RapidSVN Meld However, don&#8217;t go the websites, all of these are available in the (Default?) repositories, so you can either install them via yum, PackageKit or apt-get. Also note: RapidSVN and Meld are only needed &#8230; <a href="http://chris.iluo.net/blog/2008/12/15/x86_64-linux-c-slash-c-plus-plus-test/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I use:<br />
<a href="http://gcc.gnu.org/">gcc</a><br />
<a href="http://www.cmake.org/">cmake</a><br />
<a href="http://www.kdevelop.org/">KDevelop</a><br />
<a href="http://rapidsvn.tigris.org/">RapidSVN</a><br />
<a href="http://meld.sourceforge.net/">Meld</a></p>
<p>However, don&#8217;t go the websites, all of these are available in the (Default?) repositories, so you can either install them via <a href="http://en.wikipedia.org/wiki/Yellow_dog_Updater,_Modified">yum</a>, <a href="http://en.wikipedia.org/wiki/PackageKit">PackageKit</a> or apt-get.  Also note: RapidSVN and Meld are only needed if you want to use SVN.  Even KDevelop is not required if you have another text editor that you prefer such as gedit/vi/emacs.  If you want to create your provide your own make file then you don&#8217;t need cmake either.  </p>
<p>Anyway, so a simple application that just tests that you can do a 64 bit compile is pretty straight forward.<br />
<strong>1) Create your main.cpp file with a <em>int main(int argc, char* argv[]);</em> in it.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="cmake" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Set the minimum cmake version</span>
<span style="color: #1f3f81; font-style: bold;">cmake_minimum_required</span> <span style="color: #197d8b;">(</span><span style="color: #077807; font-sytle: italic;">VERSION</span> 2.6<span style="color: #197d8b;">)</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Set the project name</span>
<span style="color: #1f3f81; font-style: bold;">project</span> <span style="color: #197d8b;">(</span>size_test<span style="color: #197d8b;">)</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Add executable called &quot;size_test&quot; that is built from the source file</span>
<span style="color: #666666; font-style: italic;"># &quot;main.cpp&quot;. The extensions are automatically found.</span>
<span style="color: #1f3f81; font-style: bold;">add_executable</span> <span style="color: #197d8b;">(</span>size_test main.cpp<span style="color: #197d8b;">)</span></pre></div></div>

<p><strong>2) Create a CMakeLists.txt that includes your main.cpp.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span>int_ptr<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>void_ptr<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>funct_ptr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(char):        &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(short):       &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">short</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(int):         &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(long):        &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(long long):   &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;------------------------------&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(float):       &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(double):      &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(long double): &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;------------------------------&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(*int):        &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>int_ptr<span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(*void):       &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>void_ptr<span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;sizeof(*function):   &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>funct_ptr<span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bytes&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;------------------------------&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Architecture:        &quot;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>void_ptr<span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot; bit&quot;</span><span style="color: #000080;">&lt;&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>3) cd to the directory of your CMakeLists.txt and run &#8220;cmake .&#8221; and then &#8220;make&#8221;</strong><br />
<strong>4) ./size_test output:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span>char<span style="color: #7a0874; font-weight: bold;">&#41;</span>:        <span style="color: #000000;">1</span> bytes
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span>short<span style="color: #7a0874; font-weight: bold;">&#41;</span>:       <span style="color: #000000;">2</span> bytes
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span>int<span style="color: #7a0874; font-weight: bold;">&#41;</span>:         <span style="color: #000000;">4</span> bytes
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span>long<span style="color: #7a0874; font-weight: bold;">&#41;</span>:        <span style="color: #000000;">8</span> bytes
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span>long long<span style="color: #7a0874; font-weight: bold;">&#41;</span>:   <span style="color: #000000;">8</span> bytes
<span style="color: #660033;">------------------------------</span>
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span>float<span style="color: #7a0874; font-weight: bold;">&#41;</span>:       <span style="color: #000000;">4</span> bytes
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span>double<span style="color: #7a0874; font-weight: bold;">&#41;</span>:      <span style="color: #000000;">8</span> bytes
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span>long double<span style="color: #7a0874; font-weight: bold;">&#41;</span>: <span style="color: #000000;">16</span> bytes
<span style="color: #660033;">------------------------------</span>
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>int<span style="color: #7a0874; font-weight: bold;">&#41;</span>:        <span style="color: #000000;">8</span> bytes
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>void<span style="color: #7a0874; font-weight: bold;">&#41;</span>:       <span style="color: #000000;">8</span> bytes
sizeof<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:   <span style="color: #000000;">8</span> bytes
<span style="color: #660033;">------------------------------</span>
Architecture:        <span style="color: #000000;">64</span> bit</pre></div></div>

<p>As you can see this is specific to x86_64.  The beauty of gcc is that by default it compiles to the architecture it is being run on.  I had previously thought that it would be a world of pain, making sure that my compiler built the right executable code and linked in the correct libaries.  I know this project doesn&#8217;t use any special libraries, but (because of cmake?) the process is exactly the same as using cmake under 32 bit to make 32 bit executables.  You just make sure that they are there using Find*.cmake and then add them to the link step:</p>

<div class="wp_syntax"><div class="code"><pre class="cmake" style="font-family:monospace;"><span style="color: #1f3f81; font-style: bold;">SET</span><span style="color: #197d8b;">(</span>LIBRARIES
  ALUT
  OpenAL
  GLU
  SDL
  SDL_image
  SDL_net
  SDL_ttf
<span style="color: #197d8b;">)</span>
<span style="color: #666666; font-style: italic;"># Some of the libraries have different names than their Find*.cmake name</span>
<span style="color: #1f3f81; font-style: bold;">SET</span><span style="color: #197d8b;">(</span>LIBRARIES_LINKED
  alut
  openal
  GLU
  SDL
  SDL_image
  SDL_net
  SDL_ttf
<span style="color: #197d8b;">)</span>
<span style="color: #1f3f81; font-style: bold;">FOREACH</span><span style="color: #197d8b;">(</span>LIBRARY_FILE <span style="color: #b08000;">${LIBRARIES}</span><span style="color: #197d8b;">)</span>
  <span style="color: #1f3f81; font-style: bold;">Find_Package</span><span style="color: #197d8b;">(</span><span style="color: #b08000;">${LIBRARY_FILE}</span> REQUIRED<span style="color: #197d8b;">)</span>
<span style="color: #1f3f81; font-style: bold;">ENDFOREACH</span><span style="color: #197d8b;">(</span>LIBRARY_FILE<span style="color: #197d8b;">)</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Link our libraries into our executable</span>
<span style="color: #1f3f81; font-style: bold;">TARGET_LINK_LIBRARIES</span><span style="color: #197d8b;">(</span><span style="color: #b08000;">${PROJECT_NAME}</span> <span style="color: #b08000;">${LIBRARIES_LINKED}</span><span style="color: #197d8b;">)</span></pre></div></div>

<p>Note that we don&#8217;t actually have to specify the architecture for each package or even the whole executable.  This is taken care of by cmake.  Anyway, it is not some mysterious black magic, it is exactly the same as you&#8217;ve always been doing.  Cross compiling is slightly different, but basically you would just specify -m32 and make sure that you link against the 32 bit libraries instead.  If I actually bother creating another 32 bit executable in my life I&#8217;ll make sure that I document right here exactly how to do a cross compile from 64 bit.  </p>
<p>The advantages of <a href="http://en.wikipedia.org/wiki/X86-64">64 bit</a> are mmm, not so great unless you deal with really big files/memory ie. more than 4 GB.  Perhaps more practical are the extra and upgraded to 64 bit registers so you may see an increase in speed or <a href="http://en.wikipedia.org/wiki/Automatic_parallelization">parallelisation</a> of 64 bit operations, for example a game may want to use 64 bit colours (ie. 4x 16 bit floats instead of 4x 8 bit ints to represent an <a href="http://en.wikipedia.org/wiki/RGBA_color_space">rgba pixel</a>.  </p>
<p>Things to watch out for:<br />
int is still 32 bit!  If I were implementing the x86_64 version of C++ standard/gcc/in a perfect world this would have been changed to 64 bit, ie. &#8220;int&#8221; would be your native int size, it&#8217;s logical, it makes sense.  However, I do understand that this would have broken a lot of code.  The problem is, if int != architecture bits, then why have it at all, why not drop it from the standard and just have int32_t and int64_t and be done with it.  Then if a program chooses it can have:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> int32_t <span style="color: #0000ff;">int</span><span style="color: #008080;">;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> int64_t <span style="color: #0000ff;">int</span><span style="color: #008080;">;</span></pre></div></div>

<p>as it sees fit.  Anyway.<br />
Pointers are now 64 bit!  So you can use them with size_t but cannot use them with int<br />
Under linux x86_64 gcc sizeof(int*) == sizeof(function*), however, this is not guaranteed anywhere.  It may change on a certain platform/compiler.  Don&#8217;t do stuff like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> address <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>originalvariable<span style="color: #008080;">;</span></pre></div></div>

<p>gcc should warn you (you may need to <a href="http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html">turn on these warnings</a> <em>-Wformat -Wconversion</em>).  </p>
<p>All in all, if you have been writing standard code and using make/cmake it should be relatively pain free to upgrade to 64 bit.  </p>
]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2008/12/15/x86_64-linux-c-slash-c-plus-plus-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>const int vs. enum vs. #define</title>
		<link>http://chris.iluo.net/blog/2008/11/01/const-int-vs-enum-vs-define/</link>
		<comments>http://chris.iluo.net/blog/2008/11/01/const-int-vs-enum-vs-define/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 03:02:56 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/?p=12</guid>
		<description><![CDATA[Example A: int GetValue0&#40;&#41; &#123; return 10; &#125; &#160; int GetValue1&#40;&#41; &#123; return 10 + 10; &#125; &#160; int GetValue2&#40;&#41; &#123; return 10 * 10; &#125; So if all of the values 10 represent a common magic number then you &#8230; <a href="http://chris.iluo.net/blog/2008/11/01/const-int-vs-enum-vs-define/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Example A:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> GetValue0<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> GetValue1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">10</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> GetValue2<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">10</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So if all of the values 10 represent a common <a href="http://en.wikipedia.org/wiki/Magic_number_(programming)">magic number</a> then you are going to want to extract that value to one location instead of the 5 that it is in at the moment.  How do we do this?</p>
<p>Say we call the value MAGIC_NUMBER (Of course, in a real life situation you would use a better name than this, wouldn&#8217;t you?  Something like PI, GST_PERCENTAGE, NUMBER_OF_CLIENTS, etc.) we would then have this code.</p>
<p>Example B:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> GetValue0<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> MAGIC_NUMBER<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> GetValue1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> MAGIC_NUMBER <span style="color: #000040;">+</span> MAGIC_NUMBER<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> GetValue2<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> MAGIC_NUMBER <span style="color: #000040;">*</span> MAGIC_NUMBER<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Great.  The problem now is, how do we tell our program about MAGIC_NUMBER?  </p>
<p>If you originally started programming C then your first instinct may be to use:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define MAGIC_NUMBER 10</span></pre></div></div>

<p>The main problem here is that the compiler may not realise that all of the places you use MAGIC_NUMBER are linked so it may not realise that it can factor it out.  The other problem with this method is the lack of type safety.  You can use MAGIC_NUMBER with sign/unsigned ints, char, bool etc.  The compiler may not warn you about converting between these types.  For an int this isn&#8217;t really a problem, but const float PI = 3.14&#8230;f should give you a warning when you try to initialise an int to it.  This is good news as it requires you to cast if you really want to do it, which then shows other programmers than you have actually thought about what you are doing and what is happening to the value as it passes through each variable.  </p>
<p>You might be tempted to use:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> MAGIC_NUMBER <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span></pre></div></div>

<p>This is much better as it adds type safety, however you can do even better than that,</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> MAGIC_NUMBER <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span></pre></div></div>

<p>This way it isn&#8217;t &#8220;just&#8221; a global variable, when you declare it as const you are telling the compiler that it will never change in value which means that it can make all sorts of assumptions about how it will be used.  Your compiler may or may not factor out/in this value, it may or may not insert 10 + 10 and 10 * 10 into those functions for you at compile time.  Using a const int means that the compiler gets the choice of using either the value directly or using a variable containing that value, and I trust the compiler more than myself to make that decision.  Because it knows the value of MAGIC_NUMBER at compile time and knows that it will never change at runtime it can actually do the calculation and insert that value instead.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> GetValue0<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> GetValue1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">20</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> GetValue2<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">100</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The other magic number container is enum.  It varies slightly to const int as it is more for collections of values where you want to identify something by what type it is.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">enum</span> RESULT
<span style="color: #008000;">&#123;</span>
  RESULT_FILE_DOWNLOADED,
  RESULT_CONNECTION_FAILED, 
  RESULT_FILE_NOT_FOUND,
  RESULT_DISCONNECTED
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
RESULT DownloadFile<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> url<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// Pseudocode</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>could not connect<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> RESULT_CONNECTION_FAILED<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>file not found<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> RESULT_FILE_NOT_FOUND<span style="color: #008080;">;</span> 
&nbsp;
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>disconnected<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> RESULT_DICONNECTED<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> RESULT_FILE_DOWNLOADED<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In this way we can get rid of magic numbers and (In C++0x at least with <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Strongly_typed_enumerations">enum class</a>) get some type safety, your compiler will hopefully complain if you try and return an integer instead of a RESULT.</p>
]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2008/11/01/const-int-vs-enum-vs-define/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
