c++ – What is the size of a pointer?

c++ – What is the size of a pointer?

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, 16bit, 24bit, depending on where the target is located, even though the targets size is always the same (e.g. char).

Pointers generally have a fixed size, for ex. on a 32-bit executable theyre 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… Its usually pretty safe to assume theyre going to be uniform within a given executable on modern desktop OSs.

Edit: Even so, I would strongly caution against making this assumption in your code. If youre going to write something that absolutely has to have a pointers of a certain size, youd better check it!

Function pointers are a different story — see Jens answer for more info.

c++ – What is the size of a pointer?

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

Leave a Reply

Your email address will not be published.