简体   繁体   English

我们如何知道任何类型的理论上可能存在的物体的最大尺寸?

[英]How do we know the maximum size of a theoretically possible object of any type?

Here is the introduction for size_t in cppref 这是cppref中size_t的介绍

std::size_t can store the maximum size of a theoretically possible object of any type (including array). std::size_t可以存储任何类型(包括数组)的理论上可能存在的对象的最大大小。

I know the exact value is platform-dependent. 我知道确切的值取决于平台。 But who decides the size of theoretically possible object , the compiler or the operating system, or even the computer manufacturer? 但是,谁来决定theoretically possible object ,编译器或操作系统甚至计算机制造商的大小? Could the size of theoretically possible object be calculated or it's just decided by man-made rule? 可以计算理论上可能存在的物体的大小,还是由人为规则决定?

In addition, if the machine is 64-bit, does that mean the max object size could be 2^64 bytes? 另外,如果计算机是64位的,这是否意味着最大对象大小可能是2 ^ 64字节?

But who decides the size of theoretically possible object [...] ? 但是谁来决定理论上可能的物体的大小?

The author of the "implementation" gets to decide. “实现”的作者可以决定。 The implementation is a rather nebulous term that includes the compiler, run-time library, and often at least part of the OS. 该实现是一个相当模糊的术语,它包括编译器,运行时库以及通常至少一部分OS。

In addition, if the machine is 64-bit, does that mean the max object size could be 2^64 bytes? 另外,如果计算机是64位的,这是否意味着最大对象大小可能是2 ^ 64字节?

Not really. 并不是的。 You probably can't exceed 2 64 -1 bytes, but the limit may well be (normally will be, at least on machine current as of 2018 when I'm writing this) much lower than that. 您可能不能超过 2 64 -1个字节,但该限制可能会比该限制低得多(正常情况下,至少在2018年编写本文时,此限制在机器电流上)。 Many current CPUs have much smaller real limits--around 2 42 -1 is probably more realistic. 当前许多CPU的实际极限要小得多-大约2 42 -1可能更现实。

It really goes both ways. 确实是双向的。

As size_t is the return type of the sizeof operator, the type also sets the max size of an object. 由于size_tsizeof运算符的返回类型,因此该类型还设置对象的最大大小。 For sizeof to work, no object can be allowed to exceed the size that size_t can represent. 为了使sizeof正常工作,不允许任何对象超过size_t可以表示的大小。

Nothing requires a compiler to let you build a single object using all available memory. 不需要编译器就可以使用所有可用内存来构建单个对象。 There might be an upper limit, which might be the result of the choice of types for size_t and ptrdiff_t . 可能存在上限,这可能是选择size_tptrdiff_t类型的结果。

On the other hand, with current 64-bit computers, a 64-bit size_t is even way larger than required, as you cannot currently fit 16 exabytes of RAM in a computer smaller than 100 meters (cubed). 另一方面,对于当前的64位计算机,64位的size_t甚至比所需的还要大,因为您当前无法在小于100米(立方)的计算机中容纳16艾字节的RAM。 So much for "theoretically possible". “理论上可能的”这么多。 :-} :-}

if the machine is 64-bit, does that mean the max object size could be 2^64 bytes? 如果机器是64位的,这是否意味着最大对象大小可能是2 ^ 64字节?

The issue here is that a " 64-bit machine " is insufficient information to answer the question. 这里的问题是“ 64位计算机 ”没有足够的信息来回答问题。 The main limiting factor here is how much sequential memory the instruction set can address at any given time . 这里的主要限制因素是指令集在任何给定时间可以寻址多少顺序存储器 It is decided by the designer of the CPU architecture. 它由CPU架构的设计者决定。

x86 has a number of execution modes: real mode (16 bit segmented ), 32-bit mode and 64-bit mode. x86有多种执行模式:实模式( 分段的 16位),32位模式和64位模式。 In each of these modes the size of the largest sequentially addressable area of memory is different. 在这些模式的每一个中,最大的可顺序寻址存储器区域的大小是不同的。 In real mode 1MB could be addressed, but only 64KB sequentially, hence size_t is 16 bits. 在实模式下,可以寻址1MB,但只能寻址64KB,因此size_t为16位。 In other modes the entire address range can be addressed sequentially. 在其他模式下,整个地址范围可以顺序寻址。

It doesn't matter that a 64-bit processor can today physically address 48 bits, because it still operates on full 64-bit offsets and can theoretically address a 64-bit-long object. 如今,64位处理器可以物理寻址48位无关紧要,因为它仍可在完整的64位偏移量上运行,并且理论上可以寻址64位长的对象。 Hence the width of size_t there would still be 64 bits. 因此, size_t的宽度仍然会有64位。

In commodity processors the width of size_t usually corresponds to the size of the accumulator register, since that defines the maximum possible offset the CPU can address. 在商用处理器中, size_t的宽度通常对应于累加器寄存器的大小,因为它定义了CPU可以寻址的最大可能偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM