How to display the value of member function pointers?

A

Andy

Hi,

Sorry for the naive question. What I did here is to check the value of
pointers to member functions, and try to call these member function via
member functio pointers. But the program compiled with gcc gives out
segment fault. What's wrong with my code?

Here is the code:


#include <iostream>

using namespace std;

class A{
public:
int method1(int a, int b){ cout<<"Hello,world"; return 0; }
int method2(int a, int b) { return a + b; }
int method3(int x, int y) { return x * x;}
};


void conv_ptr(void * ptr, char* buf)
{
char * str = (char*)ptr;
int i;
for(i = 0; i < 4 ; i++)
sprintf(buf+i*2, "%.2x", str);
buf[8] = 0;
}


int main()
{
A a;

int (A::*ptr) (int, int) ;
char buf[9];

ptr = &A::method1;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;


ptr = &A::method2;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;

ptr = &A::method3;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
}


Thanks !

Andy
 
V

Victor Bazarov

Andy said:
Sorry for the naive question. What I did here is to check the value of
pointers to member functions, and try to call these member function via
member functio pointers. But the program compiled with gcc gives out
segment fault.
Where?

What's wrong with my code?

Several things.
Here is the code:


#include <iostream>

using namespace std;

class A{
public:
int method1(int a, int b){ cout<<"Hello,world"; return 0; }
int method2(int a, int b) { return a + b; }
int method3(int x, int y) { return x * x;}
};


void conv_ptr(void * ptr, char* buf)
{
char * str = (char*)ptr;
int i;
for(i = 0; i < 4 ; i++)
sprintf(buf+i*2, "%.2x", str);


I am not convinced that you're not overrunning the buffer here.
What's the dot for in "%.2x"? Couldn't you just write "%02x" and
make sure you pass (str & 255)? You see, 'x' assumes you passed
in an int, while you actually pass in a char...
buf[8] = 0;
}


int main()
{
A a;

int (A::*ptr) (int, int) ;
char buf[9];

ptr = &A::method1;
conv_ptr(&ptr, buf);

BTW, why are you passing in the *address* of 'ptr' and not the 'ptr'
itself?
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;


ptr = &A::method2;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;

ptr = &A::method3;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
}


V
 
K

Ken Human

Andy said:
Hi,

Sorry for the naive question. What I did here is to check the value of
pointers to member functions, and try to call these member function via
member functio pointers. But the program compiled with gcc gives out
segment fault. What's wrong with my code?

Here is the code:


#include <iostream>

using namespace std;

class A{
public:
int method1(int a, int b){ cout<<"Hello,world"; return 0; }
int method2(int a, int b) { return a + b; }
int method3(int x, int y) { return x * x;}
};


void conv_ptr(void * ptr, char* buf)
{
char * str = (char*)ptr;
int i;
for(i = 0; i < 4 ; i++)
sprintf(buf+i*2, "%.2x", str);
buf[8] = 0;
}


int main()
{
A a;

int (A::*ptr) (int, int) ;
char buf[9];

ptr = &A::method1;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;


ptr = &A::method2;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;

ptr = &A::method3;
conv_ptr(&ptr, buf);
cout<<"ptr ="<<buf<<endl;

cout<<(a.*ptr)(5,5)<<endl;
}


Thanks !

Andy


cout << &ptr; outputs the address of pointer. Your conv_ptr() function
isn't necessary.

When you point towards a member function, you don't type ptr =
&A::member; you type ptr = a.member;

Look at the following:

class A {
public:
void method1() { cout << "A::method1() called\n"; }
};


int main() {
A a;
void (A::* ptr)(void);


ptr = a.method1;
cout << &ptr << endl;
(a.*ptr)();
return 0;
}
 
V

Victor Bazarov

Ken said:
[...]
When you point towards a member function, you don't type ptr =
&A::member; you type ptr = a.member;
Huh?


Look at the following:

class A {
public:
void method1() { cout << "A::method1() called\n"; }
};


int main() {
A a;
void (A::* ptr)(void);


ptr = a.method1;
cout << &ptr << endl;
(a.*ptr)();
return 0;
}

It does not compile. I'll leave it to you to fix.

V
 
K

Ken Human

[snip]
It does not compile. I'll leave it to you to fix.

V


My mistake. I compiled that under under .NET which allows taking the
address of a bound member function to form a pointer, while ISO C++ does
not. "&class::member" is correct.
 
A

Andy

What I want is the content of the pointer, not the address of the
pointer. If we use
cout<< ptr << endl

to get it, gcc always gives out 1, this is not what I want.

Andy
 
A

Andy

If I have a program as follows,

#include <iostream>
#include <cstdlib>
using namespace std;

class A{
public:
int method1(int a, int b){ cout<<"Hello,world"; return 0; }
int method2(int a, int b) { return a + b; }
int method3(int x, int y) { return x * x;}
};

void conv_ptr(void * ptr, char* buf)
{
char * str = (char*)ptr;
int i;
for(i = 0; i < 4 ; i++)
sprintf(buf+i*2, "%02x", str);

buf[8] = 0;
}


int main()
{
A a;
int (A::*ptr) (int, int) ;

ptr = &A::method1;
cout<<"ptr ="<<ptr<<endl;
cout<<(a.*ptr)(5,5)<<endl;

ptr = &A::method2;
cout<<"ptr ="<<ptr<<endl;
cout<<(a.*ptr)(5,5)<<endl;


ptr = &A::method3;
cout<<"ptr ="<<ptr<<endl;
cout<<(a.*ptr)(5,5)<<endl;
}

Compiled with gcc-4.0, the output is :

ptr =1
Hello,world0
ptr =1
10
ptr =1
25


Here you see, the value of ptr are always 1. Can you explaini this?

Thanks,

Andy
 
V

Victor Bazarov

Andy said:
What I want is the content of the pointer, not the address of the
pointer. If we use
cout<< ptr << endl

to get it, gcc always gives out 1, this is not what I want.

What you want is to reinterpret_cast 'ptr' to a pointer to char and
not the '&ptr' and make sure your output buffer has enough room. Be
aware that the size of a pointer to member is not the same as that of
a pointer.

V
 
A

Andy

It seems that it is causes by the conversio from (void*) to (char*),
if I converted it from (void*) to (unsigned char*), the program works
correct.
Which situtation shall we use only "unsigned char*" instead of "char*"?

Thanks,

Andy
 
V

Victor Bazarov

Andy said:
If I have a program as follows,

#include <iostream>
#include <cstdlib>
using namespace std;

class A{
public:
int method1(int a, int b){ cout<<"Hello,world"; return 0; }
int method2(int a, int b) { return a + b; }
int method3(int x, int y) { return x * x;}
};

void conv_ptr(void * ptr, char* buf)
{
char * str = (char*)ptr;
int i;
for(i = 0; i < 4 ; i++)
sprintf(buf+i*2, "%02x", str);

buf[8] = 0;
}


int main()
{
A a;
int (A::*ptr) (int, int) ;

ptr = &A::method1;
cout<<"ptr ="<<ptr<<endl;
cout<<(a.*ptr)(5,5)<<endl;

ptr = &A::method2;
cout<<"ptr ="<<ptr<<endl;
cout<<(a.*ptr)(5,5)<<endl;


ptr = &A::method3;
cout<<"ptr ="<<ptr<<endl;
cout<<(a.*ptr)(5,5)<<endl;
}

Compiled with gcc-4.0, the output is :

ptr =1
Hello,world0
ptr =1
10
ptr =1
25


Here you see, the value of ptr are always 1. Can you explaini this?


The only explanation I have is that there is no operator << in 'ostream'
that takes a pointer to member, and your 'ptr' value gets converted to
something, probably 'int' and is output that way.

IIRC, there is no suitable conversion in standard C++ language that would
allow you to examine the internal representation of a pointer to member.
I may be wrong, of course.

V
 
V

Victor Bazarov

Andy said:
It seems that it is causes by the conversio from (void*) to (char*),
if I converted it from (void*) to (unsigned char*), the program works
correct.
Which situtation shall we use only "unsigned char*" instead of "char*"?

Not in any situation applicable to your attempt, anyway.

See my other reply. After careful consideration I think that what you
are trying to achieve is not allowed in standard C++.

V
 
P

Pete Becker

Victor said:
The only explanation I have is that there is no operator << in 'ostream'
that takes a pointer to member, and your 'ptr' value gets converted to
something, probably 'int' and is output that way.

It gets converted to bool. That's a standard conversion for pointers to
members.
 
V

Victor Bazarov

Pete said:
It gets converted to bool. That's a standard conversion for pointers to
members.

Thanks for this correction. I keep forgetting about the conversion to
bool...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top