What is the size of a pointer? [duplicate]
Asked Answered
G

8

89

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.

Grandioso answered 19/7, 2011 at 17:57 Comment(1)
This question is being discussed on meta.Fortuity
J
92

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.

Jillane answered 19/7, 2011 at 18:0 Comment(1)
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 ?Solenoid
B
92

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

Beltz answered 19/7, 2011 at 18:7 Comment(6)
This only applies if you're looking at the assembly. In C++ the varying sizes are hidden from your view.Fontanez
@Jay: Not true at all. sizeof(p) can give different results for different types of pointers.Fretful
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'.Fontanez
@Fontanez A pointer to a member function can change its size when casting it (Pointers to member functions are very strange animals).Gerhardine
Ah. MSVC does not represent some pointers using machine language pointers.Fontanez
@Fontanez 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.Gerhardine
C
39

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.

Carbonaceous answered 19/7, 2011 at 18:1 Comment(5)
What about a 32 bit executable on a 64 bit machine?Fleischer
It's also 32 bit. @FleischerCommendation
@Fleischer 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.Sulphathiazole
@Sulphathiazole 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.Fleischer
Directed here after searching why sizeof(ptr to int) gives me 8 while cout << ptr prints a 6 bytes address. Interesting.Perdue
F
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.

Fontanez answered 19/7, 2011 at 20:49 Comment(2)
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.Tuque
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.Fontanez
P
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

Poeticize answered 1/10, 2012 at 23:55 Comment(0)
D
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

Deterrence answered 12/6, 2015 at 14:17 Comment(0)
L
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

Liege answered 18/8, 2020 at 14:50 Comment(0)
H
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)

Hairball answered 19/7, 2011 at 18:2 Comment(1)
Except when pointing to things like a char on a word addressed machine.Tuque

© 2022 - 2024 — McMap. All rights reserved.