<?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>Safer Code - Secure Coding In C \ C++ And More.. &#187; Portability</title>
	<atom:link href="http://www.safercode.com/blog/category/portability/feed" rel="self" type="application/rss+xml" />
	<link>http://www.safercode.com/blog</link>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Tue, 05 Jan 2010 07:59:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Portable Code: How To Check If A Machine Is 32 Bit Or 64 Bit</title>
		<link>http://www.safercode.com/blog/2009/03/10/portable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html</link>
		<comments>http://www.safercode.com/blog/2009/03/10/portable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html#comments</comments>
		<pubDate>Tue, 10 Mar 2009 13:30:00 +0000</pubDate>
		<dc:creator>Shantanu Goel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Portability]]></category>
		<category><![CDATA[32–bit]]></category>
		<category><![CDATA[64–bit]]></category>
		<category><![CDATA[Architectue]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[check machine 64 bit]]></category>
		<category><![CDATA[CPP]]></category>
		<category><![CDATA[Data Model]]></category>
		<category><![CDATA[data models]]></category>
		<category><![CDATA[data sizes]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[function pointers]]></category>
		<category><![CDATA[Hardware Architecture]]></category>
		<category><![CDATA[ILP32]]></category>
		<category><![CDATA[ILP64]]></category>
		<category><![CDATA[LLP64]]></category>
		<category><![CDATA[LP32]]></category>
		<category><![CDATA[LP64]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[Portable code]]></category>
		<category><![CDATA[Programming Model]]></category>
		<category><![CDATA[SILP64]]></category>
		<category><![CDATA[writing portable code]]></category>

		<guid isPermaLink="false">http://www.safercode.com/blog/2009/03/10/portable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html</guid>
		<description><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ -->Writing portable code is very important but it is&#160;one of the aspects that most people neglect until it is too late to realize its importance. Till few years ago, most people writing code for personal computers were not worried about the data sizes on their machines. They didn&#8217;t even think whether the machines, on which [...]]]></description>
			<content:encoded><![CDATA[<!-- Powered by Shantz WP Prefix Suffix. Tech Blog: http://tech.shantanugoel.com/ Secure Programming Blog: http://www.safercode.com/blog/ Blog: http://blog.shantanugoel.com/ --><p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'><strong><a href="http://feedproxy.google.com/SaferCode" rel="alternate" type="application/rss+xml">Subscribe To Our Feed</a> | <a href="http://twitter.com/safercode" rel="nofollow">Follow Us On Twitter</a> | <a href="http://feedburner.google.com/fb/a/mailverify?uri=safercode" rel="nofollow" target="_blank">Get Updates on Email</a></strong></p>
<p>Writing portable code is very important but it is&nbsp;one of the aspects that most people neglect until it is too late to realize its importance. Till few years ago, most people writing code for personal computers were not worried about the data sizes on their machines. They didn&rsquo;t even think whether the machines, on which their code would be running, would be 32 bit or 64 bit. But the recent advent of 64 bit machines in normal every-day usage has them running helter-skelter to get their programs into shape. Many of these programs would like to run the same code base on 32 bit as well as 64 bit machines. There are many ways to do it. A few allow us to use data types that would work as expected in both machines while in other ways, they explicitly check for the architecture of the machine and carry out their tasks accordingly. Before I give you the code to run this check, let&rsquo;s see a bit of theory behind it.</p>
<p>First, let&rsquo;s be clear that the discussion we will be doing now will not always give you the &ldquo;<strong>hardware architecture</strong>&rdquo; of a machine. Rather it&rsquo;ll allow you to know the &ldquo;<strong>Programming Model</strong>&rdquo; (or <strong>Data Model</strong>)that the OS or your compiler enforces on you. What I mean is that if you run a 32 bit OS on top of your latest 64 bit processor based system, it will still mean a 32 bit&nbsp;programming model for you. Infact, if you were to compile your programs using the ancient Turbo C compiler, you&rsquo;d be in for an even bigger surprise <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .&nbsp;That said,&nbsp;ultimately the programming model&nbsp;is what you&rsquo;d be interested in knowing to make sure that your program can compile and run accurately on that particular system. The most common programming models in use are as below:</p>
<table border="1">
<tbody>
<tr>
<td>Datatype</td>
<td>LP64</td>
<td>ILP64</td>
<td>SILP64</td>
<td>LLP64</td>
<td>ILP32</td>
<td>LP32</td>
</tr>
<tr>
<td>char</td>
<td>8</td>
<td>8</td>
<td>8</td>
<td>8</td>
<td>8</td>
<td>8</td>
</tr>
<tr>
<td>short</td>
<td>16</td>
<td>16</td>
<td>64</td>
<td>16</td>
<td>16</td>
<td>16</td>
</tr>
<tr>
<td>int</td>
<td>32</td>
<td>64</td>
<td>64</td>
<td>32</td>
<td>32</td>
<td>16</td>
</tr>
<tr>
<td>long</td>
<td>64</td>
<td>64</td>
<td>64</td>
<td>32</td>
<td>32</td>
<td>32</td>
</tr>
<tr>
<td>long long</td>
<td>64</td>
<td>64</td>
<td>64</td>
<td>64</td>
<td>64</td>
<td>64</td>
</tr>
<tr>
<td>pointer</td>
<td>64</td>
<td>64</td>
<td>64</td>
<td>64</td>
<td>32</td>
<td>32</td>
</tr>
</tbody>
</table>
<p>
<span id="more-35"></span><br />
So, what do we infer from this table? The glaring issues are that we can no longer depend on the sizes of data types with which we work. If your program does not make use of the data type sizes (either explicitly or implicity, e.g. by type conversion like converting between pointers and integers), most probably you will be safe by just recompiling your code for the new machine without changing it. Otherwise, a safe bet is to base your code around either all char&rsquo;s or long long&rsquo;s because this data type will always have the same size across machines. </p>
<p>Now, onto the &ldquo;check&rdquo;. If you read the above table carefully, you will come to know that most of the data type sizes can be misleading, i.e. they can be 32 bit even on 64 bit architectures. So, most people who make the mistake of using the size of integer on a machine for their checks fall into this trap.&nbsp;However, one of them that is useful for us is &ldquo;The Pointer&rdquo;. Yes, in all these models, the pointer to a data type is always 64 bit on 64 bit architectures and 32 bit on 32 bit architectures. Hence, the check becomes rather trivial, i.e.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">n <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above expression results in n being equal to 8 on a 64 bit architecture and 4 on a 32 bit architecture. So, there it is but remember that should not try this out with function pointers because that can lead to wrong results but that is a topic for some other time. We&rsquo;ll bring you a few more articles about 32 bit vs 64 bit programming, its pitfalls, issues and areas where we can optimize and take advantage. Please let us know if you&rsquo;d like us to cover a certain aspect in particular.</p>
<p style='border:thin dotted black; padding:3mm;background-color: rgb(250,150,250);'>© <a href="http://www.safercode.com/blog/">Safer Code</a> | <a href="http://www.safercode.com/blog/2009/03/10/portable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html">Portable Code: How To Check If A Machine Is 32 Bit Or 64 Bit</a></p>



Share and Enjoy:


	<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F10%2Fportable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html&amp;title=Portable%20Code%3A%20How%20To%20Check%20If%20A%20Machine%20Is%2032%20Bit%20Or%2064%20Bit&amp;bodytext=Writing%20portable%20code%20is%20very%20important%20but%20it%20is%26nbsp%3Bone%20of%20the%20aspects%20that%20most%20people%20neglect%20until%20it%20is%20too%20late%20to%20realize%20its%20importance.%20Till%20few%20years%20ago%2C%20most%20people%20writing%20code%20for%20personal%20computers%20were%20not%20worried%20about%20the%20data%20siz" title="Digg"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F10%2Fportable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html&amp;title=Portable%20Code%3A%20How%20To%20Check%20If%20A%20Machine%20Is%2032%20Bit%20Or%2064%20Bit&amp;notes=Writing%20portable%20code%20is%20very%20important%20but%20it%20is%26nbsp%3Bone%20of%20the%20aspects%20that%20most%20people%20neglect%20until%20it%20is%20too%20late%20to%20realize%20its%20importance.%20Till%20few%20years%20ago%2C%20most%20people%20writing%20code%20for%20personal%20computers%20were%20not%20worried%20about%20the%20data%20siz" title="del.icio.us"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F10%2Fportable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html&amp;t=Portable%20Code%3A%20How%20To%20Check%20If%20A%20Machine%20Is%2032%20Bit%20Or%2064%20Bit" title="Facebook"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F10%2Fportable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html&amp;title=Portable%20Code%3A%20How%20To%20Check%20If%20A%20Machine%20Is%2032%20Bit%20Or%2064%20Bit" title="StumbleUpon"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F10%2Fportable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html&amp;title=Portable%20Code%3A%20How%20To%20Check%20If%20A%20Machine%20Is%2032%20Bit%20Or%2064%20Bit" title="Reddit"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.safercode.com%2Fblog%2F2009%2F03%2F10%2Fportable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html&amp;partner=sociable" title="Print this article!"><img src="http://www.safercode.com/blog/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>


<br/><br/>
	Tags: <a href="http://www.safercode.com/blog/tag/32bit" title="32&ndash;bit" rel="tag nofollow">32&ndash;bit</a>, <a href="http://www.safercode.com/blog/tag/64bit" title="64&ndash;bit" rel="tag nofollow">64&ndash;bit</a>, <a href="http://www.safercode.com/blog/tag/architectue" title="Architectue" rel="tag nofollow">Architectue</a>, <a href="http://www.safercode.com/blog/tag/c" title="C" rel="tag nofollow">C</a>, <a href="http://www.safercode.com/blog/tag/check-machine-64-bit" title="check machine 64 bit" rel="tag nofollow">check machine 64 bit</a>, <a href="http://www.safercode.com/blog/tag/cpp" title="CPP" rel="tag nofollow">CPP</a>, <a href="http://www.safercode.com/blog/tag/data-model" title="Data Model" rel="tag nofollow">Data Model</a>, <a href="http://www.safercode.com/blog/tag/data-models" title="data models" rel="tag nofollow">data models</a>, <a href="http://www.safercode.com/blog/tag/data-sizes" title="data sizes" rel="tag nofollow">data sizes</a>, <a href="http://www.safercode.com/blog/tag/data-types" title="data types" rel="tag nofollow">data types</a>, <a href="http://www.safercode.com/blog/tag/function-pointers" title="function pointers" rel="tag nofollow">function pointers</a>, <a href="http://www.safercode.com/blog/tag/hardware-architecture" title="Hardware Architecture" rel="tag nofollow">Hardware Architecture</a>, <a href="http://www.safercode.com/blog/tag/ilp32" title="ILP32" rel="tag nofollow">ILP32</a>, <a href="http://www.safercode.com/blog/tag/ilp64" title="ILP64" rel="tag nofollow">ILP64</a>, <a href="http://www.safercode.com/blog/tag/llp64" title="LLP64" rel="tag nofollow">LLP64</a>, <a href="http://www.safercode.com/blog/tag/lp32" title="LP32" rel="tag nofollow">LP32</a>, <a href="http://www.safercode.com/blog/tag/lp64" title="LP64" rel="tag nofollow">LP64</a>, <a href="http://www.safercode.com/blog/tag/pointers" title="pointers" rel="tag nofollow">pointers</a>, <a href="http://www.safercode.com/blog/tag/portable-code" title="Portable code" rel="tag nofollow">Portable code</a>, <a href="http://www.safercode.com/blog/tag/programming-model" title="Programming Model" rel="tag nofollow">Programming Model</a>, <a href="http://www.safercode.com/blog/tag/silp64" title="SILP64" rel="tag nofollow">SILP64</a>, <a href="http://www.safercode.com/blog/tag/writing-portable-code" title="writing portable code" rel="tag nofollow">writing portable code</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.safercode.com/blog/2009/02/24/volatile-c-keyword-myths-dispelled.html" title="Volatile: C Keyword Myths Dispelled (February 24, 2009)">Volatile: C Keyword Myths Dispelled</a> (14)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/25/generic-function-pointers-in-c-and-void.html" title="Generic Function Pointers In C And Void * (November 25, 2008)">Generic Function Pointers In C And Void *</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2008/11/11/validating-untrusted-string-inputs.html" title="Validating Untrusted String Inputs (November 11, 2008)">Validating Untrusted String Inputs</a> (1)</li>
	<li><a href="http://www.safercode.com/blog/2008/10/21/validating-untrusted-integer-inputs.html" title="Validating Untrusted Integer Inputs (October 21, 2008)">Validating Untrusted Integer Inputs</a> (6)</li>
	<li><a href="http://www.safercode.com/blog/2008/12/02/unsafe-functions-in-c-and-their-safer-replacements-strings-part-ii.html" title="Unsafe Functions In C And Their Safer Replacements: Strings Part II (December 2, 2008)">Unsafe Functions In C And Their Safer Replacements: Strings Part II</a> (8)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.safercode.com/blog/2009/03/10/portable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
