Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 232 adresses are possible to store in a pointer. Since every address is associated to 1 byte, 232 bytes give 4 GB.

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

like image 843
rullof Avatar asked Dec 25 '13 11:12

rullof


People also ask

How much memory does a pointer point to?

A pointer points into a place in memory, so it would be 32 bits on a 32-bit system, and 64 bits in 64-bit system.

Is a pointer always 4 bytes?

Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes.

Why size of pointer is 4 bytes?

Size of a pointer is fixed for a compiler. All pointer types take same number of bytes for a compiler. That is why we get 4 for both ptri and ptrc.

How many bytes is a pointer on 32-bit?

For example, the size of a char pointer in a 32-bit processor is 4 bytes, while the size of a char pointer in a 16-bit processor is 2 bytes.


1 Answers

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.

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.

With a 64 bit architecture the standard size of a pointer is doubled, so you don't have to worry anymore.

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.

like image 131
Jack Avatar answered Sep 21 '22 09:09

Jack