What Is the Size of a Pointer? What Exactly Does It Depend On

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

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).

What is the size of a pointer?

Pointers generally have a fixed size, for ex. on a 32-bit executable they're usually 32-bit. There are some exceptions, like on old 16-bit windows when you had to distinguish between 32-bit pointers and 16-bit... It's usually pretty safe to assume they're going to be uniform within a given executable on modern desktop OS's.

Edit: Even so, I would strongly caution against making this assumption in your code. If you're going to write something that absolutely has to have a pointers of a certain size, you'd better check it!

Function pointers are a different story -- see Jens' answer for more info.

Does sizeof(pointer) depend on the object type?

Please explain why sizeof(B*) return 4?

It returns 4 because size of a pointer variable is 4 in your system.

What exactly pointer to object represents in memory?

Objects reside in memory, and pointer to an object contains the starting memory address of that object. For example, if your B object has size 100 Byte, and it is placed in 1024-1123(100 Bytes) memory location, then a pointer to that object will hold the value `024 (starting address).

Is pointer to sizeof depends on the object type?

I guess you meant does pointer size depends on object type?. No, since pointers contain an address, it's size depends on address space of your system, not type of object it points to.

size of a C pointer depends on?

It depends on the CPU architecture in question. You can write portable code that takes this into consideration by using intptr_t (C99 only).

Size of pointers: Dependent factors

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.

Is the sizeof(some pointer) always equal to four?

The guarantee you get is that sizeof(char) == 1. There are no other guarantees, including no guarantee that sizeof(int *) == sizeof(double *).

In practice, pointers will be size 2 on a 16-bit system (if you can find one), 4 on a 32-bit system, and 8 on a 64-bit system, but there's nothing to be gained in relying on a given size.

How many bytes do pointers take up?

There is no fixed answer; it depends entirely on the architecture, the compiler implementation, and even the type of the pointer itself. Pointers to different types are not guaranteed to have the same size and/or representation.

For example, assume a word-addressed architecture, where the smallest addressable unit of storage is 16 bits wide (or wider). Each word can hold multiple char values; all other types take up a full word or more. On such an architecture, a char * and void * would need some extra bits to offset into the word compared to other pointer types.

Note also that a pointer type may be wider than the number of bits actually required to store an address. The original Macintosh ran on a Motorola 68000 CPU, which had a 32-bit word size, but only 24 bits on the address bus. Pointer types were 32 bits wide, leaving the upper 8 bits unused. Enterprising MacOS programmers took advantage of that to store some data to the uppermost byte of a pointer type, making the most of that precious 128 KB of RAM. Of course, Motorola eventually released a CPU with 32 address lines (the 68020), meaning all that code had to be rewritten.

On modern, commodity desktop and server hardware (read: x86), it's reasonably safe to assume that all pointer types are the same size as the native word size (32- or 64-bit), and that all pointer types have the same size and representation. Just be aware that this doesn't have to be true.

size of pointer is always equal to size of int in C compiler . Is it correct?

My senior has told that in C program size of any pointer is always equal to size of int . Is it correct?

NO, that is not correct.

There are multiple different memory models possible, often on the same system at the same time. Often systems support running ILP32 and LP64 processes at the same time. While an int is 4 bytes (32 bits) in both ILP32 and LP64, pointers are 8 bytes (64 bits) in LP64.



Related Topics



Leave a reply



Submit