91

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.

1
  • 4
    This question is being discussed on meta. Commented Aug 20, 2022 at 23:26

8 Answers 8

93

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

6 Comments

This only applies if you're looking at the assembly. In C++ the varying sizes are hidden from your view.
@Jay: Not true at all. sizeof(p) can give different results for different types of pointers.
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 A pointer to a member function can change its size when casting it (Pointers to member functions are very strange animals).
@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.
|
41

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.

5 Comments

What about a 32 bit executable on a 64 bit machine?
It's also 32 bit. @Ident
@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 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.
Directed here after searching why sizeof(ptr to int) gives me 8 while cout << ptr prints a 6 bytes address. Interesting.
17

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.

2 Comments

But the zip codes can have different size in different areas, See Are all data pointers of the same size. There is some relation in that pointers to different types can have different sizes.
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.
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

Comments

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

Comments

1

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)

1 Comment

Except when pointing to things like a char on a word addressed machine.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.