Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the size of a pointer? What exactly does it depend on?

Tags:

I searched online and while I could find a few discussions, I did not find a comprehensive description. So if anyone could form an answer which covers everything about size of a pointer, it would be of great help. The answer should at least cover following topics

  1. What does the size of a pointer depend on?
  2. What feature of architecture affects the size of a pointer? (In detail)
  3. How does the compiler affect the size of a pointer?
like image 271
sudhir vyasaraja Avatar asked Apr 20 '14 16:04

sudhir vyasaraja


People also ask

What does pointer size depend on?

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.

What's the size of a pointer?

On 32-bit machine sizeof pointer is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte.

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 a pointer in memory?

Pointers take up the space needed to hold an address, which is 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine.


1 Answers

A pointer is an abstraction provided by a high-level language; in theory it could be any width at all. It's totally at the whim of the compiler.

In practice, it's typically related to the width of the memory addresses of the underlying hardware, as that's usually the most efficient thing for the compiler to implement. There are exceptions though; for example, C++'s pointer-to-member-function does not have a direct mapping to hardware addresses, as it needs to represent two entities (the function and some notion of the type).

However, even leaving that aside, there are still complexities. For example:

  • On most modern hardware, your program will work with virtual memory addresses, rather than physical addresses (which may not be the same width). Unless you're writing kernel-space code.
  • On some architectures (e.g. x86), the underlying hardware exhibits a segmented address space. This is really complicated, but is mostly abstracted away by the OS and the virtual-memory system. If you're writing kernel-space code or code for really old x86s, you'll have to deal with it, though.
  • On current x86-64, (virtual) memory addresses are actually only 48-bits wide.
  • x86-64 supports both 32-bit and 64-bit executables.
  • You may be running inside a virtual machine, which again can do whatever if it wants (relative to the underlying physical machine).
like image 67
Oliver Charlesworth Avatar answered Oct 09 '22 07:10

Oliver Charlesworth