About sizeof of a class member function pointer [duplicate]

柔情痞子 提交于 2019-12-03 17:15:28

问题


Let's say we have a class A

class A;

and these typedefs

typedef void (A::*a_func_ptr)(void);
typedef void (*func_ptr)(void);

My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)?

For instance

int main(int argc, char *argv[])
{
  int a = sizeof(a_func_ptr);
  int b = sizeof(func_ptr);
}

回答1:


My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)?

Because pointer-to-members are implemented differently. They're not pointers under the hood. Some compilers, such as MSVC, implement them as struct with more than one members in it.

Read this interesting article:

  • Pointers to member functions are very strange animals

Note that in some compilers, they might have same size. The bottomline is: they're compiler-dependent.




回答2:


Consider the following:

#include <iostream>

class A {
public:
    virtual void foo(){ std::cout << "A::foo" << std::endl; }
    void bar(){ std::cout << "A::bar" << std::endl; }
};

class B : public A {
public:
     void foo(){  std::cout << "B::foo" << std::endl; }
     void bar(){ std::cout << "B::bar" << std::endl; }
};

typedef void (A::*a_func_ptr)(void);

int main() {
   a_func_ptr f = &A::foo;
   a_func_ptr g = &A::bar;
   B b;
   A a;
   (b.*f)();
   (b.*g)();
   (a.*f)();
   (a.*g)();
}

Output:

B::foo
A::bar
A::foo
A::bar

Both member pointers are of the same type, yet both correctly routed the call in every cases.

Somehow, the generated programme must know when a pointer to method is actually a simple method or a virtual one. Thus the runtime representation of a method pointer has to include more information to handle the second case.

Remark: the size seems to be implementation dependent (I get 8 on my system).




回答3:


Although function pointers in C and C++ can be implemented as simple addresses, so that typically sizeof(Fx)==sizeof(void *), member pointers in C++ are often implemented as "fat pointers", typically two or three times the size of a simple function pointer, in order to deal with virtual inheritance.

Source: Wikipedia

This SO answer offers additional information.




回答4:


Pointer-to-member function of class doesn't hold the "exact address" like a regular pointer does. It stores more information than regular function pointer.

So when you use sizeof to measure the size of a pointer-to-member function of a class, you should not expect that it will be same size as a regular function pointer.




回答5:


Pointers to member functions may be a data structure as pointed out in the C++ FAQ here. Also Pointers to member functions are very strange animals offers an example of how it is implemented for Visual C++.




回答6:


A related question is discussed here: Is the sizeof(some pointer) always equal to four?

See: Pointers to member functions are very strange animals for further Information.



来源:https://stackoverflow.com/questions/16062651/about-sizeof-of-a-class-member-function-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!