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 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
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;
}
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
[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.
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
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
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
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
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
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
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)
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...
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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,...
|
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...
|
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));
|
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...
|
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)...
|
by: Zach |
last post by:
Can someone please explain what this means and illustrate the
difference with some code.
Thanks,
Zach
|
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();
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |