Operating System - HP-UX
1825775 Members
1945 Online
109687 Solutions
New Discussion

sizeof (<pointers to member function>)

 
SOLVED
Go to solution
Alex Vinokur
Frequent Advisor

sizeof (<pointers to member function>)


Hi,

Pointers to member functions (methods) have
* 64 bits in 32-bit modeel
* 128 bits in 64-bit model.

1. What is the reason for that?

2. Pointer to member function has more bits than regular pointer.
What does 'pointer to member function' contain?


Thanks,

Alex

----------------------------------
HP-UX B.11.23 U ia64

aCC: HP C/aC++ B3910B A.06.25.01 [May 16 2010]

// ====== a3264.cpp: BEGIN ======
#include
#include

#define SHOW(x) std::cout << #x << ", \tsizeof = " << sizeof(x) << ", typeid = " << typeid(x).name() << std::endl

struct Dummy
{
};

typedef void (*func_t)();
typedef char Dummy::*ptr_member_t;
typedef void (Dummy::*method_t)();

int main()
{
SHOW(ptr_member_t);
SHOW(func_t);
SHOW(method_t);

return 0;
}

// ====== a3264.cpp: END ======


Compilation
> aCC +DD32 -AA a3264.cpp -o a32.out

> aCC +DD64 -AA a3264.cpp -o a64.out


Running
> a32.out
ptr_member_t, sizeof = 4, typeid = M5Dummyc
func_t, sizeof = 4, typeid = PFvvE
method_t, sizeof = 8, typeid = M5DummyFvvE

> a64.out
ptr_member_t, sizeof = 8, typeid = M5Dummyc
func_t, sizeof = 8, typeid = PFvvE
method_t, sizeof = 16, typeid = M5DummyFvvE





5 REPLIES 5
Dennis Handly
Acclaimed Contributor
Solution

Re: sizeof (<pointers to member function>)

From your above sizes:
1) Pointers to data have a 32/64 bit pointer
2) Pointers to functions have a 32/64 bit pointer to a function descriptor.
3) Pointers to member data are offsets (NULL == -1)
4) Pointers to member functions have two fields. The first is used as either a pointer to function descriptor or virtual table offset (biased by 1 byte) for virtual calls.
The second is the "this" pointer adjustment.

>2. Pointer to member function has more bits than regular pointer.

It has to have the "this" pointer adjustment.
Alex Vinokur
Frequent Advisor

Re: sizeof (<pointers to member function>)

Dennis, thank you.
Alex Vinokur
Frequent Advisor

Re: sizeof (<pointers to member function>)


> 3) Pointers to member data are offsets (NULL == -1)


HP-UX B.11.23 U ia64

aCC: HP C/aC++ B3910B A.06.25.01 [May 16 2010]

=========== a.cpp ===========
#include

struct Dummy
{
int m_int1;
char m_ch1;
double m_double1;
int m_int2;
};

int main()
{
std::cout << &Dummy::m_int1 << std::endl;
std::cout << &Dummy::m_ch1 << std::endl;
std::cout << &Dummy::m_double1 << std::endl;
std::cout << &Dummy::m_int2 << std::endl;

return 0;
}
===============================


Compilation
> aCC +DD64 -AA a.cpp

Running
> ./a.out
1
1
1
1


===================

How can we get actual offsets?

Thanks


Alex


Dennis Handly
Acclaimed Contributor

Re: sizeof (<pointers to member function>)

>std::cout << &Dummy::m_int1

Pointers to members are thingees. You can't really use them with iostream without a hammer.
What the compiler did was a boolean conversion and checked for non-NULL.

#include
struct Dummy {
int m_int1;
char m_ch1;
double m_double1;
int m_int2;
};
int main() {
int Dummy::* tm_int1 = &Dummy::m_int1;
char Dummy::* tm_ch1 = &Dummy::m_ch1;
double Dummy::* tm_double1 = &Dummy::m_double1;
int Dummy::* tm_int2 = &Dummy::m_int2;
printf("&Dummy::m_int1: %p\n", reinterpret_cast(tm_int1));
printf("&Dummy::m_ch1: %p\n", reinterpret_cast(tm_ch1));
printf("&Dummy::m_double1: %p\n", reinterpret_cast(tm_double1));
printf("&Dummy::m_int2: %p\n", reinterpret_cast(tm_int2));
}

For +DD64:
&Dummy::m_int1: 0000000000000000
&Dummy::m_ch1: 0000000000000004
&Dummy::m_double1: 0000000000000008
&Dummy::m_int2: 0000000000000010
Alex Vinokur
Frequent Advisor

Re: sizeof (<pointers to member function>)

Dennis, thanks once again.