I am finding difficulties in understanding the factors on which the size of pointer variables in C
is dependent on. I checked few references, the only information I got until now is pointer
size is dependent on the processor architecture.I would like to know the following details
- Please explain more on how the architecure impacts the pointer size.
- In general, if the pointer is of
x bits
then0 to 2^(X)-1
number of address locations should be there.I am losing track while relating the number of address locations and the actual amount of memory available to the program.
A pointer is a variable that holds the address of another memory location.
Now if you are running on a 32-bit architecture, the CPU's registers that hold memory references(and most likely, all other registers too) will be of 32-bit length; that's basically what's meant by 32-bit(the registers are of 32-bit word length) and hence a pointer(which is a memory location) would usually be 32-bits long(4 bytes)
Same applies to 64-bit CPUs, and hence the pointers in a C program compiled for 64-bit CPUs will usually have 8 bytes length(64 bits)
EDIT:
Please also note that in most modern architectures you don't really address physical memory with your code; you run and address what's called a Virtual Memory.
The basic concept is that the CPU/OS combination illude your program that you have the full address space for you.
Again, the address-space(the space you can address in memory) length will depend on how far the CPU can address locations and that (in the general case) would depend on its word-size.
Pointer size depends on a lot of factors (hardware, operating system, compiler, etc.), and not all pointer types on the same platform may have the same size. For example, there are embedded processors that use a Harvard architecture, where code and data are in separate memory areas, and each may have a different bus size (e.g., 8 bits for data, 16 bits for code). This means that object pointers (
int *
,char *
,double *
) may be 8 bits wide, but function pointers (int (*)()
) may be 16 bits wide.For another example, consider a word-addressed architecture, where the basic unit of memory is not an 8-bit byte, but a larger unit (where the width can be 16, 18, 24, 32, 36, 64, or 128 bits, or some other value; powers of 2 have proven to be convenient, but not necessary). Some of these architectures may choose to pack multiple
char
values into a single word, meaning that achar *
needs a few extra bits to specify an offset into the word.In the book C: A Reference Manual, Harbison & Steele describe an architecture with 36-bit words. Character data were stored as 7-bit ASCII values, meaning each word could hold 5 characters with one bit unused; all other types took up full words.
This is not necessarily the case, anyway you have to understand a pointer's size holds the address of a virtual address (on the OS which support it). Therefore the size of pointer depends on the memory bus width. That's why the architecture impacts the size of pointers. Note it's not true all the time: all pointers may not have the same size, for instance.