मैं void*
संकेत करने के लिए कुछ सदस्य समारोह संकेत बदलने की आवश्यकता हो जाता है (क्योंकि मैं उन्हें लुआ को पुश करने के लिए ढेर की जरूरत है, लेकिन समस्या यह संबंधित लुआ नहीं है)।एक सी ++ सदस्य समारोह सूचक कॉलिंग: इस-सूचक भ्रष्ट
मैं इसे union
का उपयोग करके करता हूं। लेकिन जब मैं सदस्य फ़ंक्शन पॉइंटर्स को void*
में परिवर्तित करता हूं और फिर वापस आ जाता हूं और फिर कक्षा के उदाहरण के साथ पॉइंटर को कॉल करने का प्रयास करता हूं, तो this
पॉइंटर दूषित हो जाता है। आश्चर्यजनक रूप से, यह समस्या नहीं होती है, अगर मैं void*
पॉइंटर को सी-स्टाइल फ़ंक्शन पॉइंटर में क्लाउड में पॉइंटर के साथ कनवर्ट करता हूं क्योंकि यह पहला पैरामीटर है।
#include <iostream>
using namespace std;
class test
{
int a;
public:
void tellSomething()
{
cout << "this: " << this << endl;
cout << "referencing member variable..." << endl;
cout << a << endl;
}
};
int main()
{
union
{
void *ptr;
void (test::*func)();
} conv1, conv2;
union
{
void *ptr;
void (*func) (test*);
} conv3;
test &t = *new test();
cout << "created instance: " << (void*) &t << endl;
// assign the member function pointer to the first union
conv1.func = &test::tellSomething;
// copy the void* pointers
conv2.ptr = conv3.ptr = conv1.ptr;
// call without conversion
void (test::*func1)() = conv1.func;
(t.*func1)(); // --> works
// call with C style function pointer invocation
void (*func3) (test*) = conv3.func;
(*func3) (&t); // --> works (although obviously the wrong type of pointer)
// call with C++ style member function pointer invocation
void (test::*func2)() = conv2.func;
(t.*func2)(); // `this' is the wrong pointer; program will crash in the member function
return 0;
}
उत्पादन है:
created instance: 0x1ff6010
this: 0x1ff6010
referencing member variable...
0
this: 0x1ff6010
referencing member variable...
0
this: 0x10200600f
referencing member variable...
zsh: segmentation fault (core dumped) ./a.out
इस संकलक (जीसीसी) में एक बग है
यहाँ कोड का टुकड़ा है कि समस्या को दर्शाता है है? मुझे पता है कि void*
और (सदस्य) फ़ंक्शन पॉइंटर्स के बीच यह रूपांतरण मानक अनुपालन नहीं है, लेकिन अजीब चीज यह है कि यह काम करता है, void*
को एक सी स्टाइल फ़ंक्शन पॉइंटर में परिवर्तित करते समय।
क्या आपने अपने memfunptr के आकार() को चेक किया है? मेरे कार्यान्वयन में यह आकार से अधिक है (शून्य *) – PlasmaHH