Posts Tagged ‘data models’

Portable Code: How To Check If A Machine Is 32 Bit Or 64 Bit

Tuesday, March 10th, 2009

Subscribe To Our Feed | Follow Us On Twitter | Get Updates on Email

Writing portable code is very important but it is 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’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’s see a bit of theory behind it.

First, let’s be clear that the discussion we will be doing now will not always give you the “hardware architecture” of a machine. Rather it’ll allow you to know the “Programming Model” (or Data Model)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 programming model for you. Infact, if you were to compile your programs using the ancient Turbo C compiler, you’d be in for an even bigger surprise ;) . That said, ultimately the programming model is what you’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:

Datatype LP64 ILP64 SILP64 LLP64 ILP32 LP32
char 8 8 8 8 8 8
short 16 16 64 16 16 16
int 32 64 64 32 32 16
long 64 64 64 32 32 32
long long 64 64 64 64 64 64
pointer 64 64 64 64 32 32

(more…)

© Safer Code | Portable Code: How To Check If A Machine Is 32 Bit Or 64 Bit

Liked this post? Get FREE Updates
Subscribe to RSS feed

Or
Enter Your E-mail ID below