Is the sizeof(some pointer) always equal to four?

前端 未结 17 1204
温柔的废话
温柔的废话 2020-11-22 11:49

For example: sizeof(char*) returns 4. As does int*, long long*, everything that I\'ve tried. Are there any exceptions to this?

17条回答
  •  死守一世寂寞
    2020-11-22 12:31

    Even on a plain x86 32 bit platform, you can get a variety of pointer sizes, try this out for an example:

    struct A {};
    
    struct B : virtual public A {};
    
    struct C {};
    
    struct D : public A, public C {};
    
    int main()
    {
        cout << "A:" << sizeof(void (A::*)()) << endl;
        cout << "B:" << sizeof(void (B::*)()) << endl;
        cout << "D:" << sizeof(void (D::*)()) << endl;
    }
    

    Under Visual C++ 2008, I get 4, 12 and 8 for the sizes of the pointers-to-member-function.

    Raymond Chen talked about this here.

提交回复
热议问题