Do All Pointers Have the Same Size in C++

Do I have the guarantee that all pointers have the same size in bytes?

No. There is no guarantee in the standard.

There are some exception is some systems. Although it's fixed in many typical systems and depends on the architecture of that system. For example in 32-bit systems pointers are 4 bytes.

By the way, uintptr_t can hold pointers (Maybe we can assume it has the maximum size of a pointer in the current system):

uintptr_t unsigned integer type capable of holding a pointer

Why must all pointers to structs be of the same size?

The answer is very simple: struct and union types can be declared as opaque types, ie: without an actual definition of the struct or union details. If the representation of pointers was different depending on the structures' details, how would the compiler determine what representation to use for opaque pointers appearing as arguments, return values, or even just reading from or storing them to memory.

The natural consequence of the ability to manipulate opaque pointer types is all such pointers must have the same representation. Note however that pointers to struct and pointers to union may have a different representation, as well as pointers to basic types such as char, int, double...

Another distinction regarding pointer representation is between pointers to data and pointers to functions, which may have a different size. Such a difference is more common in current architectures, albeit still rare outside operating system and device driver space. 64-bit for function pointers seems a waste as 4GB should be amply sufficient for code space, but modern architectures take advantage of this extra space to store pointer signatures to harden code against malicious attacks. Another use is to take advantage of hardware that ignores some of the pointer bits (eg: x86_64 ignores the top 16 bits) to store type information or to use NaN values unmodified as pointers.

Furthermore, the near/far/huge pointer attributes from legacy 16 bit code were not correctly addressed by this remark in the C Standard as all pointers could be near, far or huge. Yet the distinction between code pointers and data pointers in mixed model code was covered by it and seems still current on some OSes.

Finally, Posix mandates that all pointers have the same size and representation so mixed model code should quickly become a historical curiosity.

It is arguable that architectures where the representation is different for different data types are vanishingly rare nowadays and it be high time to clean up the standard and remove this option. The main objection is support for architectures where the addressable units are large words and 8-bit bytes are addressed using extra information, making char * and void * larger than regular pointers. Yet such architectures make pointer arithmetics very cumbersome and are quite rare too (I personally have never seen one).

Why are pointers are of same size in C

Because all you need is 64 bits to index a memory address and all the data types you've listed only need to index a memory address (where the given data 'starts' in memory). Note that this is true for C, but in C++ e.g., member function pointers might be more than that.

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.

why size of struct is differs from size of struct pointer

There is no relationship between the size of a pointer type and the size of the type it points to. That sizeof (int) and sizeof (int *) yield the same value on your system is coincidence (they don’t on my system).

A pointer stores an address (either physical or virtual) - on a system like x86 with a flat memory model, all pointer types are the same size (usually the native word size, either 32 or 64 bits on most desktop systems). However, there are some oddball architectures out there where pointer types may have different sizes from each other.

The only requirements1 are:

  • char * and void * have the same size and representation
  • pointers to qualified or unqualified versions of compatible types have the same size and representation (i.e., int * and const int * and volatile int * have the same size)
  • all struct pointer types have the same size and representation;
  • all union pointer types have the same size and representation;

Nothing is guaranteed beyond that.


  1. C 2011 online draft, § 6.2.5, clause 28.



Related Topics



Leave a reply



Submit