34

Isn't a pointer just an address? Or I'm missing something?

I tested with several types of pointers:

  • pointers to any variables is the same (8B on my platform)
  • pointers to functions are the same size, as pointers to variables (8B again)
  • pointers to functions with different parameters - still the same (8B)

BUT pointers to member functions are bigger - 16B on my platform.

Three things:

  1. Why are pointers to member functions bigger? What more information do they need?
  2. As far as I know, the standard says nothing about the size of a pointer, except that 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?
  3. Are there any other examples for pointers, that are with different size (I mean, for standard platforms, not some rare and special ones)?
Community
  • 1
  • 1
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • 3
    Essentially, the standard doesn't require data pointers, function pointers and member function pointers to all have the same size. If you want to know why they don't have the same size on your platform, you'll have to ask the maintainers of your C++ compiler. http://www.parashift.com/c++-faq/cant-cvt-memfnptr-to-voidptr.html http://www.parashift.com/c++-faq/cant-cvt-fnptr-to-voidptr.html – Cubic Aug 17 '12 at 13:48
  • @KirilKirov no problem. I'm not always being sarcastic :) –  Aug 17 '12 at 14:02

5 Answers5

38

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:

Multiple inheritance

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: enter image description here

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

VLL
  • 9,634
  • 1
  • 29
  • 54
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • I have taken the liberty of inserting the text from the Standard I think you were looking for. I hope that's alright. – jogojapan Aug 17 '12 at 13:55
  • Note that as far as the standard is concerned, an `int*` may be smaller than a `void*`; for some very old C implementations, it was. And all that's guaranteed for a `void*` is non-member data: there's nothing that guarantees, for example, that a pointer to member data is not larger than a `void*` (although I can't imagine a reasonable implementation where it would be). – James Kanze Aug 17 '12 at 14:24
  • 1
    This: going to be implemented as something like a pointer to an object is obviously not correct. The object pointer is provided at the point where the method is called. But I think you just mismangled your words a bit. I would just re-phrase it to indicate more information is required to support polymorphic behavior. – Martin York Aug 17 '12 at 17:01
  • 1
    I think `foo(&a);` should be `A_foo(&a);` at your code line which says: `A a; foo(&a);` – Megidd Nov 22 '17 at 18:07
7

Basicly because they need to support polymorphic behavior. See a nice article by Raymond Chen.

Alexander Chertov
  • 2,070
  • 13
  • 16
2

Some explanations may be found here : The underlying representation of member function pointers

Although pointers to members behave like ordinary pointers, behind the scenes their representation is quite different. In fact, a pointer to member usually consists of a struct containing up to four fields in certain cases. This is because pointers to members have to support not only ordinary member functions, but also virtual member functions, member functions of objects that have multiple base classes, and member functions of virtual base classes. Thus, the simplest member function can be represented as a set of two pointers: one holding the physical memory address of the member function, and a second pointer that holds the this pointer. However, in cases like a virtual member function, multiple inheritance and virtual inheritance, the pointer to member must store additional information. Therefore, you can't cast pointers to members to ordinary pointers nor can you safely cast between pointers to members of different types. Blockquote

incises
  • 1,045
  • 9
  • 7
0

I'd guess it has something to do with this pointer... That is, each member function must also have the pointer for the class they are in. The pointer then makes the function a bit bigger in size.

weggo
  • 134
  • 3
  • 10
  • :) I thought the same way, weggo, but it's not true. For example, because initializing such pointer has nothing to do with any object, but only with the class declaration. That showed me, that I'm wrong. – Kiril Kirov Aug 17 '12 at 14:12
0

Some of the main reasons for representing pointers to member functions as {this, T (*f)()} are:

  • It has simpler implementation in the compiler than implementing pointers to member functions as T (*f)()

  • It does not involve runtime code generation nor additional bookkeeping

  • It performs reasonably well compared to T (*f)()

  • There isn't enough demand from C++ programmers for the size of pointers to member functions to be equal to sizeof(void*)

  • Runtime code generation during execution is de-facto a taboo for C++ code currently