简体   繁体   English

指针可以指向4GB后的地址吗?

[英]Can a pointer point to an address after 4GB?

If we compile and execute the code below: 如果我们编译并执行以下代码:

int *p;
printf("%d\n", (int)sizeof(p));

it seems that the size of a pointer to whatever the type is 4 bytes, which means 32 bit, so 2 32 adresses are possible to store in a pointer. 似乎指向任何类型的指针的大小是4个字节,这意味着32位,因此2 32个地址可以存储在指针中。 Since every address is associated to 1 byte, 2 32 bytes give 4 GB. 由于每个地址与1个字节相关联,因此2 个32个字节为4 GB。

So, how can a pointer point to the address after 4 GB of memory? 那么,4 GB内存后指针如何指向地址? And how can a program use more than 4 GB of memory? 一个程序如何使用超过4 GB的内存?

By principle, if you can't represent an address which goes over 2^X-1 then you can't address more than 2^X bytes of memory. 原则上,如果你不能代表超过2^X-1的地址,那么你不能处理超过2^X字节的内存。

This is true for x86 even if some workarounds have been implemented and used (like PAE ) that allows to have more physical memory even if with limits imposed by the fact that these are more hacks than real solutions to the problem. 对于x86来说也是如此,即使已经实现并使用了一些变通方法(如PAE ),这些变通方法允许拥有更多的物理内存,即使受到这些事实的限制,这些限制也比问题的真正解决方案更为严重。

With a 64 bit architecture the standard size of a pointer is doubled, so you don't have to worry anymore. 使用64位架构时,指针的标准大小加倍,因此您不必再担心了。

Mind that, in any case, virtual memory translates addresses from the process space to the physical space so it's easy to see that a hardware could support more memory even if the maximum addressable memory from the process point of view is still limited by the size of a pointer. 请注意,在任何情况下,虚拟内存都会将地址从进程空间转换为物理空间,因此很容易看出硬件可以支持更多内存,即使从进程角度来看最大可寻址内存仍然受到大小的限制一个指针。

To access >4GB of address space you can do one of the following: 要访问> 4GB的地址空间,您可以执行以下操作之一:

  • Compile in x86_64 (64 bit) on a 64 bit OS. 在64位操作系统上编译x86_64(64位)。 This is the easiest. 这是最简单的。
  • Use AWE memory . 使用AWE内存 AWE allows mapping a window of memory which (usually) resides above 4GB. AWE允许映射一个(通常)高于4GB的内存窗口。 The window address can be mapped and remapped again and again. 可以反复映射和重新映射窗口地址。 Was used in large database applications and RAM drives in the 32 bit era. 在32位时代用于大型数据库应用程序和RAM驱动器。

Note that a memory address where the MSB is 1 is reserved for the kernel. 请注意,MSB为1的内存地址是为内核保留的。 Windows allows under several conditions to use up to 3GB (per process), the top 1GB is always for the kernel. Windows允许在几种情况下使用高达3GB(每个进程),最高1GB始终用于内核。

By default a 32 bit process has 2GB of user mode address space. 默认情况下,32位进程具有2GB的用户模式地址空间。 It's possible to get 3GB via a special linker flag (in VS: /LARGEADDRESSAWARE). 通过特殊的链接器标志(在VS:/ LARGEADDRESSAWARE中)可以获得3GB。

"How can a pointer point to the address after 4GB of memory?" “在4GB内存之后,指针如何指向地址?”

There is a difference between the physical memory available to the processor and the "virtual memory" seen by the process. 处理器可用的物理内存与进程看到的“虚拟内存”之间存在差异。 A 32 bit process (which has a pointer of size 4 bytes) is limited to 4GB however the processor maintains a mapping (controlled by the OS) that lets each process have its own memory space, up to 4GB each. (其具有的大小4个字节的指针)一个32位的处理限制为4GB然而处理器维护映射(由OS控制),可以让每个进程具有其自身的存储器空间中,高达每4GB。

That way 8GB of memory could be used on a 32 bit system, if there were two processes each using 4GB. 这样,如果有两个进程每个使用4GB,则可以在32位系统上使用8GB内存。

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

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