Solaris 10 上的 64 位与 32 位 C 代码

发布于 2024-09-26 21:56:13 字数 449 浏览 8 评论 0原文

在我的 Solaris 10 update 9 系统上,会产生以下命令:

#isainfo -b
64 

但是,如果我用 C 语言创建以下程序并包含 limit.h,我会得到:

#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

我期望得到更大的结果,因为系统在 64 位上运行。这看起来像是 32 位结果。这是编译器问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

各自安好 2024-10-03 21:56:13

64 位平台有多种编程模型,http://www.unix .org/version2/whatsnew/lp64_wp.html,包括:

  • ILP64(其中 int、long 和指针为 64 位)
  • LP64(其中 int 为 32 位,而 long 和指针为 64 位)

64 -bit Solaris 10 使用 LP64 模型 (http://www.sun.com/software/solaris/faqs/64bit.xml#q4):

问:使用的数据模型是什么
Solaris 操作系统?

A:LP64是事实上的产业
标准。 L代表长和
P代表指针。两者都是
64 位,而 int 是 32 位。

除了“64 位编程模型:为什么选择 LP64?”上面引用的论文中,您可能需要查看 Raymond Chen 对 Win64 为什么选择 LLP64 模型的解释,因为它可能有助于支持 unix.org 文档中的各种基本原理和论点:http://blogs.msdn.com/b/oldnewthing/archive/2005/01/31/363790。 ASPX

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

等待我真够勒 2024-10-03 21:56:13

无论平台如何,“int”类型在 gcc 上都是 32 位。 “long”类型在 32 位平台上为 32 位,在 64 位平台上为 64 位。

为了减少歧义,您可以使用 C99 类型:

#include <stdint.h>

int32_t i32;
int64_t i64;

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;
┈┾☆殇 2024-10-03 21:56:13

来自 gcc 文档

64位环境设置int为32
位和长整型以及指向 64 位的指针
并为 AMD 的 x86-64 生成代码
架构。

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.

℉絮湮 2024-10-03 21:56:13

您可以在 Solaris 10 上编译 32 位或 64 位程序。默认情况下,它们编译为 32 位。

使用 GCC 和更新的 Sun 编译器时,选项“-m32”和“-m64”指示使用哪个选项。因此,尝试:

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

然后运行:

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

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...
$
离笑几人歌 2024-10-03 21:56:13

如果您想要最大整数类型的大小,即 intmax_t

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

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

它始终至少为 2^63 - 1。

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.

看轻我的陪伴 2024-10-03 21:56:13

首先,您必须确保编译器在 64 位模式下运行。某些编译器默认为 32 位目标平台模式,即使它们能够生成 64 位代码。

其次,一些编译器更喜欢 64 位类型模型,其中 int 类型仍然是 32 位。 GCC实际上就是其中之一。因此,您对 int 类型在 64 位模式下成为 64 位类型的期望是完全没有根据的。

同样,一切都取决于编译器,并且仅取决于编译器(以及编译器设置)。您对操作系统所做的事情完全无关。您可以将 Solaris 更新到 237 位或 1001 位版本,但 GCC 将继续生成 32 位代码,直到 GCC 默认值更改或直到您明确请求不同的目标平台。

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.

浅浅淡淡 2024-10-03 21:56:13

如果您想查看“long”类型(在 32 位架构上为 32 位,在 64 位架构上为 64 位,而不是始终为 32 位的“int”),您可以打印

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

:我用“-m64”编译时是这个

Max long on this system is: 9223372036854775807

,用“-m32”编译时是这个

Max long on this system is: 2147483647

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文