What Is the Size of a Pointer

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.

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

Size of pointer, pointer to pointer in C

char *c[] = {"Mahesh", "Ganesh", "999", "333"};

c is an array of char* pointers. The initializer gives it a length of 4 elements, so it's of type char *[4]. The size of that type, and therefore of c, is 4 * sizeof (char*).

char *a;

a is a pointer of type char*.

char **cp[] = {c+3, c+2, c+1, c};

cp is an array of char** pointers. The initializer has 4 elements, so it's of type char **[4]. It size is 4 * sizeof (char**).

char ***cpp = cp;

cpp is a pointer to pointer to pointer to char, or char***. Its size is sizeof (char***).

Your code uses %d to print the size values. This is incorrect -- but it happens to work on your system. Probably int and size_t are the same size. To print a size_t value correctly, use %zu -- or, if the value isn't very large, you can cast it to int and use %d. (The %zu format was introduced in C99; there might still be some implementations that don't support it.)

The particular sizes you get:

sizeof a == 4
sizeof c == 16
sizeof cp == 16
sizeof cpp == 4

are specific to your system. Apparently your system uses 4-byte pointers. Other systems may have pointers of different sizes; 8 bytes is common. Almost all systems use the same size for all pointer types, but that's not guaranteed; it's possible, for example, for char* to be larger than char***. (Some systems might require more information to specify a byte location in memory than a word location.)

(You'll note that I omitted the parentheses on the sizeof expressions. That's legal because sizeof is an operator, not a function; its operand is either an expression (which may or may not be parenthesized) or a type name in parentheses, like sizeof (char*).)

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.

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.

size of pointers in c language

For size of a pointer I would mean the number of bits (or bytes) necessary to hold that pointer in memory, or send its value across some channel, and this is (possibly) different from the size of the object the pointer points to.

Then, it can be assumed as fairly true the affirmation that pointer sizes are commonly 32 or 64 bits (4 or 8 bytes), in the sense that the systems much talked about (computers, smartphones and tablets) have pointers of that size.

But there are other systems around, smaller like DOS-based PCs or microcontrollers for embedded systems, where a pointer can be 16 bits wide or even less, and bigger systems with bus width of, say, 128 bits.

I worked in the past with the Intel 8051 CPU, which had pointers 8 bits wide, 16 bits wide, and 24 bits wide. Of course they were not freely mixable... That CPU was indeed quite strange, having about 3-4 different (and little) areas of memory; a "specialized" pointer could point only in its special area, while the 24 bit wide one could point to any area because in the upper byte there was a "selector".

Another matter is the size of the object the pointer points to. On normal computers it is a byte, but sometimes, on certain systems, it is impossible to address bytes on odd addresses in this way, so pointer arithmetic gets complicated. The 8051 (I like it!) had even pointers pointing to bits! So the size of the pointed object was actually an eight of byte, and incrementing the pointer by one could, or could not, address a different memory location than before.



Related Topics



Leave a reply



Submit