WORD_BIT vs LONG_BIT

What is the difference between WORD_BIT and LONG_BIT? And which one should I use to find out the word size of my system? Both return different results

➜  ~ getconf LONG_BIT
64
➜  ~ getconf WORD_BIT
32
➜  ~ 

I also read in some other post that a reliable way to find out word size can be to print the size of void. This gives me 8 bytes.

printf("%dn", (int)sizeof(void*)); 

Will this always be equal to the result returned by getconf LONG_BIT?

Sorry, sneaking one more question here – word size is related to the CPU architecture. Will OS also work on the same word size that CPU architecture has defined?

Asked By: 0Nicholas

||

POSIX defines WORD_BIT and LONG_BIT as the number of bits in objects of types int and long respectively. The size of an object of type void * is the size of a pointer to data¹.

32-bit int and 64-bit long and pointers is the standard model for Linux on 64-bit x86; it’s known as LP64 (see the AMD64 extensions to the System V ABI).

There is no guaranteed link between these sizes; the only size equality guarantee C provides is that sizeof(char) is 1. Windows on x64 for example uses 32-bit long and int, and 64-bit pointers. (There are other size requirements: C guarantees that sizeof(short)sizeof(int)sizeof(long), short and int are at least 16 bits, and long is at least 32 bits; POSIX additionally requires that int and long are at least 32 bits in length.)

The most significant object size in terms of matching an operating system an CPU is the pointer size (how int and long are handled is mostly a concern for the compiler), but the relationship between the operating system and the CPU’s pointer size isn’t always straightforward either. An operating system has to use a pointer size supported by the CPU, but CPUs can support multiple pointer sizes… For example, 64-bit x86 CPUs² can run 64-bit operating systems (Windows x64, 64-bit Linux, etc.), 32-bit operating systems (Windows x32, 32-bit Linux, OS/2, etc.), and 16-bit operating systems (such as MS-DOS). Even among 64-bit operating systems, there can be variations, as mentioned above (32-bit long on Windows x64, 64-bit long on 64-bit Linux), and even the “native” pointer size might not be what you expect (the Linux x32 ABI, and SPARC, use 32-bit pointers in user space). Historically, released versions of Windows on the 64-bit Alpha platform were 32-bit operating systems.


¹ In some environments (non-Unix, as far as I’m aware), pointers to data aren’t always the same size as pointers to functions.

² With appropriate firmware support, until Intel releases X86S CPUs.

Answered By: Stephen Kitt

To add to the other information:
the phrase “the size of a pointer” is ambiguous. 
In the x86 memory models “Medium” and “Compact” models,
the size of a pointer to data and the size of a pointer to code
are not the same. 
The type void * refers to a pointer to data.

Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.