Isn't a pointer just an address? Or I'm missing something?
I tested with several types of pointers:
BUT pointers to member functions are bigger - 16B on my platform.
Three things:
void*
must be able to "contain" any pointer type. In other words, any pointer must be able to be casted to void*
, right? If so, then why sizeof( void* )
is 8, while sizeof
a pointer to member function is 16?A function-pointer is a 4-byte elementary item . Function-pointers have the same capabilities as procedure-pointers, but are 4 bytes in length instead of 8 bytes. Function-pointers are thus more easily interoperable with C function pointers.
Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed. It is common to all data types like int *, float * etc.
No, it is not correct. The size of an int and the size of a pointer vary across platforms, and they are not necessarily the same. Indeed, on modern 64-bit consumer hardware, you're likely to find 32-bit int s and 64-bit pointers. Show activity on this post.
Generally yes, All pointers to anything, whether they point to a int or a long or a string or an array of strings or a function, point to a single memory address, which is the same size on a machine.
In the most normal situation, you can pretty much think of
struct A { int i; int foo() { return i; } }; A a; a.foo();
as
struct A { int i; }; int A_foo( A* this ) { return this->i; }; A a; A_foo(&a);
(Starting to look like C
, right?) So you would think the pointer &A::foo
would just be the same as a normal function pointer. But there are a couple of complications: Multiple inheritance, and virtual functions.
So imagine we have:
struct A {int a;}; struct B {int b;}; struct C : A, B {int c;};
It might be laid out like this:
As you can see, if you want to point to the object with an A*
or a C*
, you point to the start, but if you want to point to it with a B*
you have to point somewhere in the middle.
So if C
inherits some member function from B
and you want to point to it then call the function on a C*
, it needs to know to shuffle the this
pointer. That information needs to be stored somewhere. So it gets lumped in with the function pointer.
Now for every class that has virtual
functions, the compiler creates a list of them called a virtual table. It then adds an extra pointer to this table to the class (vptr). So for this class structure:
struct A { int a; virtual void foo(){}; }; struct B : A { int b; virtual void foo(){}; virtual void bar(){}; };
The compiler might end up making it like this:
So a member function pointer to a virtual function actually needs to be an index into the virtual table. So a member function pointer actually needs 1) possibly a function pointer, 2) possibly an adjustment of the this
pointer, and 3) possibly a vtable index. To be consistent, every member function pointer needs to be capable of all of these. So that's 8
bytes for the pointer, 4
bytes for the adjustment, 4
bytes for the index, for 16
bytes total.
I believe this is something that actually varies a lot between compilers, and there are a lot of possible optimizations. Probably none actually implements it the way I've described.
For a lot of detail, see this (scroll to "Implementations of Member Function Pointers").
Basicly because they need to support polymorphic behavior. See a nice article by Raymond Chen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With