gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:52:48 25 4
gpt4 key购买 nike

根据 numerous answers在这里,longint 在 C 和 C++ 的通用平台(Windows 和 Linux,32 位和 64 位)上都是 32 位大小。(我知道那里没有标准,但在实践中,这些是观察到的尺寸。)

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

最佳答案

来自C99 rationale (PDF)关于第 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. [...]

关于c - long 和 int 通常大小相同的历史背景是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6388725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com