long 和 int 通常大小相同的历史背景是什么?

发布于 2024-11-15 17:39:22 字数 335 浏览 3 评论 0原文

根据此处的众多答案longint是在 C 和 C++ 的通用平台(Windows 和 Linux,32 和 64 位)上,大小都是 32 位。(我知道没有标准,但实际上,这些是观察到的大小。)

所以我的问题是,这是怎么发生的?为什么我们有两种尺寸相同的类型?我以前总是假设 long 大多数时候是 64 位,而 int 是 32 位。我并不是说它“应该”是一种方式或另一种方式,我'我只是好奇我们是怎么来到这里的。

According to numerous answers here, long and int are both 32 bits in size on common platforms in C and C++ (Windows & Linux, 32 & 64 bit.) (I'm aware that there is no standard, but in practice, these are the observed sizes.)

So my question is, how did this come about? Why do we have two types that are the same size? I previously always assumed long would be 64 bits most of the time, and int 32. I'm not saying it "should" be one way or the other, I'm just curious as to how we got here.

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

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

发布评论

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

评论(5

醉生梦死 2024-11-22 17:39:22

来自 C99 基本原理 (PDF) 第 6.2.5 节:

[...] 在 20 世纪 70 年代,16 位 C(对于
PDP-11) 第一个代表文件
16位整数信息,
很快就被磁盘淘汰了
进步。人们转向 32 位
文件系统,首先使用int[2]
的构造不仅是
尴尬,但也效率不高
可移植到 32 位硬件。

为了解决这个问题,long类型
被添加到语言中,尽管
这需要 PDP-11 上的 C
生成多个操作
模拟 32 位算术。即使作为
32位小型机问世
除了 16 位系统之外,人们仍然
使用 int 提高效率,保留
long 适用于较大整数的情况
确实需要,因为 long
在 16 位上效率明显较低
系统。 shortlong 都是
添加到 C,使 short 可用
对于 16 位,long 对于 32 位,以及
int 以方便性能。
没有想要锁定的愿望
将数字 16 或 32 转换为语言,如
C 编译器至少存在了
24 位和 36 位 CPU,而是
提供可用于的名称
根据需要为 32 位。

PDP-11 C 可能是
使用 int 重新实现为 32 位,
从而避免了 long 的需要;但
这会让人们改变
大多数使用 intshort
性能严重下降
在 PDP-11 上。除了
对源代码的潜在影响,
对现有目标代码的影响和
数据文件会更糟,甚至
1976 年。到了 20 世纪 90 年代,
软件的安装基础,以及
动态链接的广泛使用
图书馆,改变的影响
中公共数据对象的大小
现有环境如此之高
虽然很少有人能忍受
创建时可能是可以接受的
新环境。因此,很多商家,
为了避免命名空间冲突,有
将 64 位整数添加到 32 位整数中
C 环境使用新名称,
其中long long 是最
广泛使用。 [...]

From the C99 rationale (PDF) on section 6.2.5:

[...] In the 1970s, 16-bit C (for the
PDP-11) first represented file
information with 16-bit integers,
which were rapidly obsoleted by disk
progress. People switched to a 32-bit
file system, first using int[2]
constructs which were not only
awkward, but also not efficiently
portable to 32-bit hardware.

To solve the problem, the long type
was added to the language, even though
this required C on the PDP-11 to
generate multiple operations to
simulate 32-bit arithmetic. Even as
32-bit minicomputers became available
alongside 16-bit systems, people still
used int for efficiency, reserving
long for cases where larger integers
were truly needed, since long was
noticeably less efficient on 16-bit
systems. Both short and long were
added to C, making short available
for 16 bits, long for 32 bits, and
int as convenient for performance.
There was no desire to lock the
numbers 16 or 32 into the language, as
there existed C compilers for at least
24- and 36-bit CPUs, but rather to
provide names that could be used for
32 bits as needed.

PDP-11 C might have been
re-implemented with int as 32-bits,
thus avoiding the need for long; but
that would have made people change
most uses of int to short or
suffer serious performance degradation
on PDP-11s. In addition to the
potential impact on source code, the
impact on existing object code and
data files would have been worse, even
in 1976. By the 1990s, with an immense
installed base of software, and with
widespread use of dynamic linked
libraries, the impact of changing the
size of a common data object in an
existing environment is so high that
few people would tolerate it, although
it might be acceptable when creating a
new environment. Hence, many vendors,
to avoid namespace conflicts, have
added a 64-bit integer to their 32-bit
C environments using a new name, of
which long long has been the most
widely used. [...]

巡山小妖精 2024-11-22 17:39:22

从历史上看,C 中的大多数尺寸和类型都可以追溯到 PDP-11 架构。它有字节、字(16 位)和双字(32 位)。当C和UNIX被转移到另一台机器(我认为是Interdata 832)时,字长是32位。为了保持源代码兼容,定义了 longint,以便严格

sizeof(short)sizeof(int)sizeof(long)

现在大多数机器都以 sizeof(int) = sizeof(long) 结束,因为 16 位不再方便,但如果需要的话,我们还有很长的时间可以得到 64 位。

更新 严格来说我应该说“编译器”,因为不同的编译器实现者可以对相同的指令集架构做出不同的决定。例如,海湾合作委员会和微软。

Historically, most of the sizes and types in C can be traced back to the PDP-11 architecture. That had bytes, words (16 bits) and doublewords (32 bits). When C and UNIX were moved to another machine (the Interdata 832 I think), the word length was 32 bits. To keep the source compatible, long and int were defined so that, strictly

sizeof(short)sizeof(int)sizeof(long).

Most machines now end up with sizeof(int) = sizeof(long) because 16 bits is no longer convenient, but we have long long to get 64 bits if needed.

Update strictly I should have said "compilers" because different compiler implmentors can make different decisions for the same instruction set architecture. GCC and Microsoft, for example.

无边思念无边月 2024-11-22 17:39:22

早在 70 年代末和 80 年代初,许多架构都是 16 位,因此通常 char 是 8 位,int 是 16 位,long 是 32 位。在 80 年代末,普遍转向 32 位架构,因此 int 变成了 32 位,但长期保持在 32 位。

在过去 10 年里,出现了向 64 位计算发展的趋势,我们现在有几种不同的模型,最常见的是 LP64,其中 int 仍然是 32 位,long 现在是 64 位。

底线:不要对不同整数类型的大小做出任何假设(当然标准中定义的除外),如果您需要固定大小的类型,请使用

Back in the late 70s and early 80s many architectures were 16 bit, so typically char was 8 bit, int was 16 bit and long was 32 bit. In the late 80s there was a general move to 32 bit architectures and so int became 32 bits but long remained at 32 bits.

Over the last 10 years there has been a move towards 64 bit computing and we now have a couple of different models, the most common being LP64, where ints are still 32 bits and long is now 64 bits.

Bottom line: don't make any assumptions about the sizes of different integer types (other than what's defined in the standard of course) and if you need fixed size types then use <stdint.h>.

缱绻入梦 2024-11-22 17:39:22

据我了解,C 标准要求 long 至少为 32 位长,并且至少与 int 一样长。另一方面,int 总是(我认为)等于架构的本机字大小。

请记住,在制定标准时,32 位计算机并不常见;最初,int 可能是本机 16 位,而 long 在 32 位时长度会是原来的两倍。

As I understand it, the C standard requires that a long be at least 32 bits long, and be at least as long as an int. An int, on the other hand, is always (I think) equal to the native word size of the architecture.

Bear in mind that, when the standards were drawn up, 32-bit machines were not common; originally, an int would probably have been the native 16-bits, and a long would have been twice as long at 32-bits.

诺曦 2024-11-22 17:39:22

在16位操作系统中,int是16位,long是32位。迁移到 Win32 后,两者都变为 32 位。迁移到 64 位操作系统时,最好保持 long 大小不变,这样在 64 位编译时不会破坏现有代码。新类型(如 Microsoft 特定的 __int64、size_t 等)可以在 64 位程序中使用。

In 16-bit operating systems, int was 16-bit and long was 32-bit. After moving to Win32, both become 32 bit. Moving to 64 bit OS, it is a good idea to keep long size unchanged, this doesn't break existing code when it compiled in 64 bit. New types (like Microsoft-specific __int64, size_t etc.) may be used in 64 bit programs.

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