Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of pointers: Dependent factors

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 then 0 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.
like image 394
Vivek Maran Avatar asked Oct 28 '12 14:10

Vivek Maran


People also ask

What does pointer size depend on?

It depends upon different issues like Operating system, CPU architecture etc. 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. So for a specific architecture pointer size will be fixed.

How do you determine the size of a pointer variable?

The size of any type of pointer in C is equal to the size of the integer variable in that system. For example, in a 16 bit system size of integer is 2 bytes which is same as size of pointer.

What determines the size of a pointer in C?

As we already know, the size of the pointer in C is dependent only on the word size of a particular system. So, the size of a pointer to a pointer should have the usual values, that is, 2 bytes for a 16-bit machine, 4 bytes for a 32-bit machine, and 8 bytes for a 64-bit machine.

What is the size of pointer data type?

On 32-bit machine sizeof pointer is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte. Regardless of what data type they are pointing to, they have fixed size.


2 Answers

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.

like image 66
Fingolfin Avatar answered Sep 28 '22 12:09

Fingolfin


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 a char * 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.

like image 25
John Bode Avatar answered Sep 28 '22 11:09

John Bode