tags:

views:

136

answers:

6

On my Solaris 10 update 9 system 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?

+1  A: 

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.

Vitor Py
+5  A: 

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;
vanza
+1  A: 

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.

Matthew Flaschen
This will actually display -1. If you want a correct display, use %lld instead of %d in the printf statement format.
jlliagre
Thanks, @jlliagre.
Matthew Flaschen
You're welcome, %jd is less known but indeed even better.
jlliagre
+7  A: 

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

Michael Burr
+1 for interesting links. (For the record, while I understand what Windows did with LLP64, I think that LP64 is the saner model. It's just unfortunate that the Win “galaxy” codebase wasn't up to that transition; just too many bad practices had built up.)
Donal Fellows
+1 for LP64 versus ILP64
Paul R
+1  A: 

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...
$
Jonathan Leffler
A: 

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.

AndreyT