85

Is the size of a pointer the same as the size as the type it's pointing to, or do pointers always have a fixed size? For example...

int x = 10;
int * xPtr = &x;
char y = 'a';
char * yPtr = &y;

std::cout << sizeof(x) << "\n";
std::cout << sizeof(xPtr) << "\n";
std::cout << sizeof(y) << "\n";
std::cout << sizeof(yPtr) << "\n";

What would the output of this be? Would sizeof(xPtr) return 4 and sizeof(yPtr) return 1, or would the 2 pointers actually return the same size?

The reason I ask this is because the pointers are storing a memory address and not the values of their respective stored addresses.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MGZero
  • 5,812
  • 5
  • 29
  • 46
  • 4
    This question is being [discussed on meta](https://meta.stackoverflow.com/questions/419937). – cigien Aug 20 '22 at 23:26

8 Answers8

93

Function Pointers can have very different sizes, from 4 to 20 bytes on an x86 machine, depending on the compiler. So the answer is no - sizes can vary.

Another example: take an 8051 program. It has three memory ranges and thus has three different pointer sizes, from 8 bit, 16 bit, 24 bit, depending on where the target is located, even though the target's size is always the same (e.g., char).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jens
  • 6,173
  • 2
  • 24
  • 43
  • 1
    This only applies if you're looking at the assembly. In C++ the varying sizes are hidden from your view. – Jay Jun 29 '12 at 19:00
  • 4
    @Jay: Not true at all. `sizeof(p)` can give different results for different types of pointers. – Nemo Oct 02 '12 at 00:09
  • 2
    Sorry, I was not clear and specific. On a specific x86 machine the pointer size will not vary. On a different architecture or compiler the pointer size can be different. Being very careful to use the strict definition of 'vary'. – Jay Oct 02 '12 at 11:49
  • 2
    @Jay A pointer to a member function can change its size when casting it ([Pointers to member functions are very strange animals](http://blogs.msdn.com/b/oldnewthing/archive/2004/02/09/70002.aspx)). – IInspectable Apr 14 '14 at 08:53
  • Ah. MSVC does not represent some pointers using machine language pointers. – Jay Apr 14 '14 at 13:01
  • 3
    @Jay This is not MSC-specific: All C++ compilers supporting multiple inheritance have to adjust the object pointer when invoking a member function pointer, and thus provide appropriate facilities to calculate the correct offset at run-time. Many compilers implement member function pointers through structures holding the required information. The only exception I know of is Digital Mars: It generates a thunk that performs the object pointer adjustment and member function pointers point to this stub code instead. This allows **all** pointers to be the same size. – IInspectable Apr 21 '14 at 13:40
91

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.

Community
  • 1
  • 1
Nathan Monteleone
  • 5,430
  • 29
  • 43
  • maybe my question is off-topic but why should we distinguish between 32 bit application and 64 bit application if they do not make this assumption in the code ? – kingsjester Oct 15 '22 at 21:51
38

On 32-bit machine sizeof pointer is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte. Regardless of what data type they are pointing to, they have fixed size.

peeyush
  • 2,841
  • 3
  • 24
  • 43
  • 23
    What about a 32 bit executable on a 64 bit machine? – Ident Sep 08 '15 at 14:58
  • It's also 32 bit. @Ident – learner Jul 08 '21 at 07:44
  • @Ident Why do you think "32 bit executable on a 64 bit machine, pointer is 32 bit " is wrong? I just compiled a 32bit C program on my 64bit Ubunut, `sizeof` output is 4 bytes. – Rick Aug 10 '21 at 11:10
  • 3
    @Rick it is about what word size it is compiled or and not about the machine it runs on, so if it is compiled for 32 bit then it will be 32 bit on a 64 machine, which makes the original answer incorrect. – Ident Aug 11 '21 at 12:30
  • Directed here after searching why sizeof(ptr to int) gives me 8 while cout << ptr prints a 6 bytes address. Interesting. – user174174 Jun 20 '22 at 02:36
16

To answer your other question. The size of a pointer and the size of what it points to are not related. A good analogy is to consider them like postal addresses. The size of the address of a house has no relationship to the size of the house.

Jay
  • 13,803
  • 4
  • 42
  • 69
  • 1
    But the zip codes can have different size in different areas, See [Are all data pointers of the same size](http://stackoverflow.com/questions/1241205/are-all-data-pointers-of-the-same-size-in-one-platform). There is some relation in that pointers to different types can have different sizes. – Bo Persson Jun 29 '12 at 10:20
  • 1
    If you look at the CPU there are different kinds of addressing methods (depends on the processor). The smallest encode the address relative to where the instruction is instead of giving an absolute address. Some are relative to a CPU register. They're a little larger than the first type (if you include the register). The largest have an absolute address. These are usually the largest since they need to have enough bits to encode the entire processor address space. C and C++ hide these details from you. You use absolute addresses and the compiler determines how it can get what you want. – Jay Jun 29 '12 at 14:17
9

Pointers are not always the same size on the same architecture.

You can read more on the concept of "near", "far" and "huge" pointers, just as an example of a case where pointer sizes differ...

http://en.wikipedia.org/wiki/Intel_Memory_Model#Pointer_sizes

3

They can be different on word-addressable machines (e.g., Cray PVP systems).

Most computers today are byte-addressable machines, where each address refers to a byte of memory. There, all data pointers are usually the same size, namely the size of a machine address.

On word-adressable machines, each machine address refers instead to a word larger than a byte. On these, a (char *) or (void *) pointer to a byte of memory has to contain both a word address plus a byte offset within the addresed word.

http://docs.cray.com/books/004-2179-001/html-004-2179-001/rvc5mrwh.html

Markus Kuhn
  • 983
  • 9
  • 10
1

Recently came upon a case where this was not true, TI C28x boards can have a sizeof pointer == 1, since a byte for those boards is 16-bits, and pointer size is 16 bits. To make matters more confusing, they also have far pointers which are 22-bits. I'm not really sure what sizeof far pointer would be.

In general, DSP boards can have weird integer sizes.

So pointer sizes can still be weird in 2020 if you are looking in weird places

0

The size of a pointer is the size required by your system to hold a unique memory address (since a pointer just holds the address it points to)

J T
  • 4,946
  • 5
  • 28
  • 38