Can a pointer point to an address after 4 GB?

If we compile and execute the following code:

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

It seems that the size of the pointer to any type is 4 bytes, which means 32 bits, so 2 pointers of 32 can be stored in the pointer. Since each address is associated with 1 byte, 2 32 bytes give 4 GB.

So, how can a pointer point to an address after 4 GB of memory? And how can a program use more than 4 GB of memory?

+6
source share
3 answers

Basically, if you cannot imagine an address that passes 2^X-1 , you cannot address more than 2^X bytes of memory.

This is true for x86, even if some workarounds were implemented and used (for example, PAE ), which allows you to have more physical memory, even if with the restrictions imposed by the fact that it is more hacks than real solutions to the problem.

In 64-bit architecture, the standard pointer size is doubled, so you no longer have to worry.

Remember that in any case, virtual memory translates addresses from process space to physical space, so it’s easy to see that the hardware can support more memory, even if the maximum addressable memory from the point of view of the process is still limited by the size of the pointer.

+6
source

For access> 4 GB of address space, you can do one of the following:

  • Compile 64-bit OS on x86_64 (64 bit). This is the easiest.
  • Use AWE memory . AWE allows you to display a memory window that (usually) is above 4 GB. The window address can be mapped and reassigned again and again. It was used in large database applications and RAM devices in the 32-bit era.

Note that the memory address where MSB is 1 is reserved for the kernel. Windows allows you to use up to 3 GB (for each process) under several conditions, the top 1 GB is always for the kernel.

By default, a 32-bit process has 2 GB of user-mode address space. It is possible to get 3 GB through a special linker flag (in VS: / LARGEADDRESSAWARE).

+2
source

"How can a pointer point to an address after 4 GB of memory?"

There is a difference between the physical memory available to the processor and the “virtual memory” observed by the process. A 32-bit process (which has a 4-byte pointer) is limited to 4 GB, but the processor supports a mapping (OS-controlled), which allows each process to have its own memory space, up to 4 GB each.

Thus, 8 GB of memory could be used on a 32-bit system if each of the two processes used 4 GB.

+2
source

All Articles