<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Portable Code: How To Check If A Machine Is 32 Bit Or 64 Bit</title>
	<atom:link href="http://www.safercode.com/blog/2009/03/10/portable-code-how-to-check-if-a-machine-is-32-bit-or-64-bit.html/feed" rel="self" type="application/rss+xml" />
	<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>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Thu, 18 Feb 2010 05:11:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Find The Offset Of An Element In A Structure In C- offsetof() &#124; Safer Code - Secure Coding In C C++ And More..</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#comment-1651</link>
		<dc:creator>Find The Offset Of An Element In A Structure In C- offsetof() &#124; Safer Code - Secure Coding In C C++ And More..</dc:creator>
		<pubDate>Tue, 05 May 2009 13:38:34 +0000</pubDate>
		<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#comment-1651</guid>
		<description>[...] above snippet will work, but not always (Hint). Many people use a much simplified form, which does not involve any pointer arithmetic: unsigned [...]</description>
		<content:encoded><![CDATA[<p>[...] above snippet will work, but not always (Hint). Many people use a much simplified form, which does not involve any pointer arithmetic: unsigned [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unwind</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#comment-685</link>
		<dc:creator>unwind</dc:creator>
		<pubDate>Mon, 06 Apr 2009 18:29:43 +0000</pubDate>
		<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#comment-685</guid>
		<description>@Shantanu: Oh, I got 450 ms of Internet fame! :) Amazing ... But I think &quot;the&quot; is stretching it a bit. True though, I did write gentoo (the file manager, for picky people who only associate the word with the Linux distro). Glad you know it!

Btw, I find the backslash in &quot;C \ C++&quot; horrible ... I find &quot;C/C++&quot; horrible too; they&#039;re two separate languages, may I humbly suggest the word &quot;and&quot;, to cover both?</description>
		<content:encoded><![CDATA[<p>@Shantanu: Oh, I got 450 ms of Internet fame! <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Amazing &#8230; But I think &#8220;the&#8221; is stretching it a bit. True though, I did write gentoo (the file manager, for picky people who only associate the word with the Linux distro). Glad you know it!</p>
<p>Btw, I find the backslash in &#8220;C \ C++&#8221; horrible &#8230; I find &#8220;C/C++&#8221; horrible too; they&#8217;re two separate languages, may I humbly suggest the word &#8220;and&#8221;, to cover both?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zombie No. 5</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#comment-682</link>
		<dc:creator>Zombie No. 5</dc:creator>
		<pubDate>Mon, 06 Apr 2009 17:19:46 +0000</pubDate>
		<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#comment-682</guid>
		<description>I don&#039;t quite get the point of the check in the first place. &quot;64-bit&quot; is most of the time marketing blather. I think even today no 64-bit machine has a true 64-bit address and data bus. They can handle at most 48 to 56-bit and the rest is just zero padded. Also, the C compiler doesn&#039;t have to expose or limit itself to the actual hardware architecture. Consider Java which always emulates a 64-bit environment. A C compiler can use an arbitrary ABI with long being 32 or 64 bit regardless of the CPU. See, long long is already an &quot;emulated&quot; integer type on 32-bit architectures, so, for example, modifying such integers cannot be atomic.
It is also not necessarily a good idea to use 64-bit integers on 64-bit architectures. Many people seem to think  it will be automatically faster. No, it wasted bandwidth and memory. Heck, it is even an issue when pointers are 64-bit wide. You rarely need more than 2 GiB address space or memory but you&#039;ll always have the overhead. With code heavy on pointer handling, such as linked lists, hash tables, you get twice as much overhead. So it can actually be faster to use indices and limit them to 32 bit or maybe even 16 bit. Modern CPUs are much faster when they can operate on cached or prefetched data. So while it&#039;s not a problem to use much more memory nowadays, it is still a good idea to keep data compact and local to take as much advantage of memory as possible.

Clean code that isn&#039;t &quot;optimized&quot; to death, rarely needs to know sizes of integers or pointers. You just use the sizeof operator and respect the integers&#039; extreme values instead of making assumptions. Since the compiler knows these compile-time constants and many optimization techniques, well-written code will be optimized without having to do that manually. For example, a mask operation like &quot;&amp; 0xffffU&quot; will be completely skipped for 16-bit integers but if you write it out, your code will still work with 32-bit or 36-bit integers.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t quite get the point of the check in the first place. &#8220;64-bit&#8221; is most of the time marketing blather. I think even today no 64-bit machine has a true 64-bit address and data bus. They can handle at most 48 to 56-bit and the rest is just zero padded. Also, the C compiler doesn&#8217;t have to expose or limit itself to the actual hardware architecture. Consider Java which always emulates a 64-bit environment. A C compiler can use an arbitrary ABI with long being 32 or 64 bit regardless of the CPU. See, long long is already an &#8220;emulated&#8221; integer type on 32-bit architectures, so, for example, modifying such integers cannot be atomic.<br />
It is also not necessarily a good idea to use 64-bit integers on 64-bit architectures. Many people seem to think  it will be automatically faster. No, it wasted bandwidth and memory. Heck, it is even an issue when pointers are 64-bit wide. You rarely need more than 2 GiB address space or memory but you&#8217;ll always have the overhead. With code heavy on pointer handling, such as linked lists, hash tables, you get twice as much overhead. So it can actually be faster to use indices and limit them to 32 bit or maybe even 16 bit. Modern CPUs are much faster when they can operate on cached or prefetched data. So while it&#8217;s not a problem to use much more memory nowadays, it is still a good idea to keep data compact and local to take as much advantage of memory as possible.</p>
<p>Clean code that isn&#8217;t &#8220;optimized&#8221; to death, rarely needs to know sizes of integers or pointers. You just use the sizeof operator and respect the integers&#8217; extreme values instead of making assumptions. Since the compiler knows these compile-time constants and many optimization techniques, well-written code will be optimized without having to do that manually. For example, a mask operation like &#8220;&amp; 0xffffU&#8221; will be completely skipped for 16-bit integers but if you write it out, your code will still work with 32-bit or 36-bit integers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derek Jones</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#comment-336</link>
		<dc:creator>Derek Jones</dc:creator>
		<pubDate>Wed, 11 Mar 2009 12:11:08 +0000</pubDate>
		<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#comment-336</guid>
		<description>@Shantanu:  The problem with using sizeof is that padding bits/bytes may exist in the type, rendering the result useless.  Checking the value of the macros INTPTR_MIN/MAX (in stdint.h) is the only reliable way of obtaining information on the size of pointers.</description>
		<content:encoded><![CDATA[<p>@Shantanu:  The problem with using sizeof is that padding bits/bytes may exist in the type, rendering the result useless.  Checking the value of the macros INTPTR_MIN/MAX (in stdint.h) is the only reliable way of obtaining information on the size of pointers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</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#comment-334</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Wed, 11 Mar 2009 11:35:34 +0000</pubDate>
		<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#comment-334</guid>
		<description>@unwind: Thanks for the input. I will update your and Derek&#039;s suggestions to the post in a short while.
PS: I guess you are &quot;The&quot; Emil (of gentoo)? :)</description>
		<content:encoded><![CDATA[<p>@unwind: Thanks for the input. I will update your and Derek&#8217;s suggestions to the post in a short while.<br />
PS: I guess you are &#8220;The&#8221; Emil (of gentoo)? <img src='http://www.safercode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unwind</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#comment-333</link>
		<dc:creator>unwind</dc:creator>
		<pubDate>Wed, 11 Mar 2009 08:56:47 +0000</pubDate>
		<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#comment-333</guid>
		<description>Bleh. The blog engine ate my brackets. Maybe it supports HTML entities? If so, this looks right:
#include &lt;limits.h&gt;
If not, the header to include is limits.h.</description>
		<content:encoded><![CDATA[<p>Bleh. The blog engine ate my brackets. Maybe it supports HTML entities? If so, this looks right:<br />
#include &lt;limits.h&gt;<br />
If not, the header to include is limits.h.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: unwind</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#comment-332</link>
		<dc:creator>unwind</dc:creator>
		<pubDate>Wed, 11 Mar 2009 08:55:49 +0000</pubDate>
		<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#comment-332</guid>
		<description>I&#039;d suggest doing
#include 

n = CHAR_BIT * sizeof (void *);

To capture word-addressed machines, where CHAR_BIT will be larger than 8. Silently assuming CHAR_BIT is eight is not a good way to write portable code.</description>
		<content:encoded><![CDATA[<p>I&#8217;d suggest doing<br />
#include </p>
<p>n = CHAR_BIT * sizeof (void *);</p>
<p>To capture word-addressed machines, where CHAR_BIT will be larger than 8. Silently assuming CHAR_BIT is eight is not a good way to write portable code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shantanu Goel</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#comment-330</link>
		<dc:creator>Shantanu Goel</dc:creator>
		<pubDate>Wed, 11 Mar 2009 04:59:54 +0000</pubDate>
		<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#comment-330</guid>
		<description>@Derek: yeah. I just read through the document. So, this approach might have issues on some machines which do not follow the data models listed above. Do you think using an int * would be better than a void * then? (Because word-addressed processors face this different addressing issue only for pointers characters/bytes and void, which is supposed to have a representation identical to characters.) But that also leads me to believe that there wouldn&#039;t be a &quot;true&quot; portable piece to check this across _all_ architectures because even apart from word-addressing, &quot;some&quot; architectures with their own compilers might choose to represent pointers differently. So, we can probably have something that works across the common programming models that one thinks their program would work on (e.g. the ones listed above are the ones that common-workstation processors use)

PS: Your searchable standard and associated commentary is great. I found the statement related to void * representation also on your site.</description>
		<content:encoded><![CDATA[<p>@Derek: yeah. I just read through the document. So, this approach might have issues on some machines which do not follow the data models listed above. Do you think using an int * would be better than a void * then? (Because word-addressed processors face this different addressing issue only for pointers characters/bytes and void, which is supposed to have a representation identical to characters.) But that also leads me to believe that there wouldn&#8217;t be a &#8220;true&#8221; portable piece to check this across _all_ architectures because even apart from word-addressing, &#8220;some&#8221; architectures with their own compilers might choose to represent pointers differently. So, we can probably have something that works across the common programming models that one thinks their program would work on (e.g. the ones listed above are the ones that common-workstation processors use)</p>
<p>PS: Your searchable standard and associated commentary is great. I found the statement related to void * representation also on your site.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derek Jones</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#comment-328</link>
		<dc:creator>Derek Jones</dc:creator>
		<pubDate>Tue, 10 Mar 2009 23:55:49 +0000</pubDate>
		<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#comment-328</guid>
		<description>Nice try, but this approach will not work on processors that use word addressing, such as the Cray PVP.  Many DSPs use word addressing and they get around this problem by defining the character types to be 16 or 32-bits.

Check out the &#039;Common implementation&#039; discussion in: http://c0x.coding-guidelines.com/3.6.pdf</description>
		<content:encoded><![CDATA[<p>Nice try, but this approach will not work on processors that use word addressing, such as the Cray PVP.  Many DSPs use word addressing and they get around this problem by defining the character types to be 16 or 32-bits.</p>
<p>Check out the &#8216;Common implementation&#8217; discussion in: <a href="http://c0x.coding-guidelines.com/3.6.pdf" rel="nofollow">http://c0x.coding-guidelines.com/3.6.pdf</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
