Considering Data Type Storage Size: 32-bit vs. 64-bit
I read a very interesting (and very excellent) article the other day called "A Collection of Examples of 64-bit Errors in Real Programs" by Andrey Karpov. It got me to thinking about differences in 32-bit and 64-bit programming in C, and some fundamental concepts that involve this issue.
This may seem very basic, but easy to overlook: pointers are the same size, no matter the type to which they point. Pointer size is also dependent on the data-type model. The data-type model can be ILP32 or LP64, as mentioned in the Sun article mentioned in the sources list below. The name ILP32 tells us that integers, longs, and pointers are 32-bit in size in 32-bit applications. Similarly, the name LP64 tells us that longs and pointers are 64-bit in size in 64-bit applications, but that integers and floats are the same size as before (32-bits).
With a 32-bit pointer, we can address up to 4GB (since 2^30 is 1GB, which is 30 bits, then we multiply by 2^2 (an additional 2 bits), in order to get the 32-bit representation). But what happens if we're used to storing an address that's 32-bits in length in an integer, and never thought about the case where addressing could be more than 32-bits? We could have an application that we wrote in a 32-bit mindset, which has instructions for using pointers and casting them as integers, or storing them as integers. This would be ok if we only had addresses that were less than 4GB, but anything above that would cause problems because of the truncation of the bits after bit 31.
Andrey Karpov (mentioned in the source list below) talks about this case in his article, specifically in the section "Example 2 - Unnecessary Type Conversions". Another variant of this truncation issue is in the section "Example 4 - Confusion of int and int*".
I must say that before reading Andrey Karpov's article, I was not aware of this issue, but now it's something that I definitely think about.
Sources:
"A Collection of Examples of 64-bit Errors in Real Programs". Karpov, A. http://www.codeproject.com/KB/cpp/Examples-64-bit-Errors.aspx
"Converting 32-bit Applications Into 64-bit Applications: Things to Consider". http://developers.sun.com/solaris/articles/ILP32toLP64Issues.html














