473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to display the value of member function pointers?

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[i]);
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

Jul 23 '05 #1
13 2120
Andy wrote:
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]);
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[i] & 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
Jul 23 '05 #2
Andy wrote:
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[i]);
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;
}
Jul 23 '05 #3
Ken Human wrote:
[...]
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
Jul 23 '05 #4
[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.
Jul 23 '05 #5
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

Jul 23 '05 #6
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[i]);

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

Jul 23 '05 #7
Andy wrote:
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
Jul 23 '05 #8
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

Jul 23 '05 #9
Andy wrote:
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[i]);

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
Jul 23 '05 #10
Andy wrote:
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
Jul 23 '05 #11
Victor Bazarov wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #12
Pete Becker wrote:
Victor Bazarov wrote:

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.


Thanks for this correction. I keep forgetting about the conversion to
bool...
Jul 23 '05 #13
Victor Bazarov wrote:

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


So do I. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
by: LinusLee | last post by:
class CAnalyzer { // attribute private: public: // behavior private: void NullFunc(NODE *pNode); void TraverseNode(NODE* pNode, void (*preFunc)(NODE *node), void (*postFunc)(NODE *node));
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
40
by: Zach | last post by:
Can someone please explain what this means and illustrate the difference with some code. Thanks, Zach
1
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.