开发者

64 bit vs 32 bit C code on Solaris 10

On my Solaris 10 update 9 syste开发者_开发百科m the following command yields:

#isainfo -b
64 

But if I create the following program in C with limits.h is included I get:

#include <stdio.h>
#include <limits.h>

int main(void)
{ 
     printf("Maximum integer value on this system is = %d\n", INT_MAX);
}
gcc on64.c -o on64
./on64

Maximum integer value on this system is = 2147483647

I was expecting a much bigger result because the system runs on 64 bit. This seems like a 32 bit result. Is this a compiler issue?


There are a variety of programming models for 64-bit platforms, http://www.unix.org/version2/whatsnew/lp64_wp.html, including:

  • ILP64 (where int, long, and pointers are 64-bit)
  • LP64 (where int is 32-bit, while long and pointers are 64-bit)

64-bit Solaris 10 uses the LP64 model (http://www.sun.com/software/solaris/faqs/64bit.xml#q4):

Q: What is the data model used for the Solaris Operating System?

A: LP64 is the de facto industry standard. The L represents long and the P represents pointer. Both are 64-bit, whereas int is 32-bit.

In addition to the "64-Bit Programming Models: Why LP64?" paper referenced above, you might want to look at Raymond Chen's explanation for why Win64 chose the LLP64 model, as it might help bolster the various rationales and arguments in the unix.org document: http://blogs.msdn.com/b/oldnewthing/archive/2005/01/31/363790.aspx


The "int" type is 32-bits on gcc regardless of the platform. The "long" type is 32 bits on 32-bit platforms, and 64 bits on 64-bit platforms.

To be less ambiguous, you could use C99 types:

#include <stdint.h>

int32_t i32;
int64_t i64;


From the gcc documentation:

The 64-bit environment sets int to 32 bits and long and pointer to 64 bits and generates code for AMD's x86-64 architecture.


You can compile programs on Solaris 10 for 32-bit or 64-bit. By default, they are compiled 32-bit.

Using both GCC and the more recent Sun compilers, the options '-m32' and '-m64' dictate which option is used. Hence, try:

$ gcc -m64 -o on64-64 on64.c
$ gcc -m32 -o on64-32 on64.c

Then run:

$ file on64 on64-32 on64-64
...take a look see...
$ ./on64-64
...take a look see...
$ ./on64-32
...as you originally found...
$


If you want the size of the largest integral type, that's intmax_t:

#include <stdio.h>
#include <stdint.h>

int main(void)
{ 
     printf("Maximum integer value on this system is = %jd\n", INTMAX_MAX);
}

This will always be at least 2^63 - 1.


Firstly, you have to make sure you are running your compiler in 64-bit mode. Some compilers default to 32-bit target platform mode, even if they are capable of generating 64-bit code.

Secondly, some compilers prefer to 64-bit type model where type int remains 32-bit. GCC is actually one of them. So, your expectations for int type becoming 64-bit type in 64-bit mode are completely unfounded.

Again, everything depends on the compiler and only on the compiler (and on the compiler settings). What you did to your OS is totally irrelevant. You can update your Solaris to 237-bit or 1001-bit version, but GCC will continue to generate 32-bit code, until the GCC default is changed or until you explicitly request a different target platform.


If you want to see the "long" type (which is 32bits on 32-bit architecture and 64-bits on 64-bit architecture, as opposed to "int" which is always 32-bit), you could have printed:

printf("max long = %ld", LONG_MAX);

that gives me this when compiled with '-m64'

Max long on this system is: 9223372036854775807

and this when compiled with '-m32'

Max long on this system is: 2147483647
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜