Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8061887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:29:19+00:00 2026-06-05T10:29:19+00:00

I need to convert some member function pointers to void* pointers (because I need

  • 0

I need to convert some member function pointers to void* pointers (because I need to push them to the Lua stack, but the problem is not Lua related).

I do this using a union. But when I convert the member function pointers to a void* and back again and then try to call the pointer with an instance of the class, the this pointer gets corrupted. Strangly, this problem doesn’t happen, if I convert the void* pointer back into a C-style function pointer with a pointer to the class as it’s first parameter.

Here is piece of code that demonstrates the problem:

#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;
}

That is the output:

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

Is this a bug in the compiler (GCC)? I know that this conversion between void* and (member) function pointers is not standard compliant, but the odd thing is, that it works, when converting the void* to a C style function pointer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:29:21+00:00Added an answer on June 5, 2026 at 10:29 am

    Add these two lines to your code and the answer will be clear:

    cout << "sizeof(void*)=" << sizeof(conv1.ptr) << endl;
    cout << "sizeof(test::*)=" << sizeof(conv1.func) << endl;
    

    The reason is simple. Consider:

    class Base1
    {
     public:
     int x;
     void Foo();
     Base1();
    };
    
    class Base2
    {
     public:
     float j;
     void Bar();
     Base2();
    };
    
    class Derived : public Base1, public Base2
    {
     Derived();
    };
    

    When you call Foo on a Derived, the this pointer must point to Base1::x. But when you call Bar on a Derived, the this pointer must point to Base2::j! So a pointer to a member function must include both the address of the function and an “adjustor” to correct the this pointer to point to an instance of the correct type of class that the function expects as the this pointer.

    You are losing the adjuster, causing the this pointer to be adjusted randomly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert an enumeration member (its value, not the identifier) to a
I need to convert some files to PDF and then attach them to an
I need to convert some strings, and pull out the two first integers e.g:
I need to convert some code done by someone else, to work in my
I need to convert an open delegate (one in which the Target is not
Not sure if this question makes for some poor performance down the track, but
I need convert some Mac Numbers(00163e2fbab7) into Mac String. (with :) Is there some
I need to convert some code compiled with ARMASM to gcc(code sourcery GCC-4.6.2 eabi).
I need to convert some 2-letter (neutral) identifiers (like en for English) so something
I need to convert some color ( in Hex ) to Brush. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.