13

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.
3
  • It is 4 bytes on a 32 bit machine and 8 bytes on a 64 bit machine. This is not guaranteed by the Standard and even sometimes false in the real life.
    – ouah
    Oct 28, 2012 at 14:34
  • 2
    It is not guaranteed at all that 32-bit-cpu = 32bit pointers, etc. A small-footprint 32bit arch CPU on OS/400 uses 128-bit pointers and a single global 128-bit virtual address space. Your entering assumptions are wrong. See this question for more info if you're interested.
    – WhozCraig
    Oct 28, 2012 at 14:37
  • Many 64-bit machines can run both 32-bit and 64-bit executables. Oct 28, 2012 at 17:05

3 Answers 3

10

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.

4

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.

2
  • Does the specification dictate anything regarding the sizeof pointers ? Apr 27, 2017 at 16:46
  • 1
    @Talespin_Kit: Not as far as specific size. See N1570, 6.2.5/28. In short, char * and void * must have the same size and representation as each other, pointers qualified and unqualified versions of compatible types must have the same size and representation, pointers to all struct types must have the same size and representation (same for union types). Apart from that, it's up to the implementation. But there's no requirement for a minimum size or representation.
    – John Bode
    Apr 27, 2017 at 17:22
0

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. It is 4 bytes on a 32 bit machine and 8 bytes on a 64 bit machine. I would like to know the following details

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.

1
  • 4
    The size of the pointer does not depend on the memory bus width. The pointers in an 8086 and an 8088 were the same size, but the memory bus width of an 8086 was 16 bits and that of an 8088 was 8 bits. Oct 28, 2012 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.