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 8595291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:24:00+00:00 2026-06-12T00:24:00+00:00

Yesterday I was doing some research on dynamic loading of shared objects and about

  • 0

Yesterday I was doing some research on dynamic loading of shared objects and about getting pointers to functions.

I have been told many times that sharing pointers to functions through void pointers is forbidden by the ISO C++ standard and is still and issue to resolve.

After reading Johan Petterson’s artitle “about the problem with dlsym” I understand better the reasons, and I also understand that being forbidden by the standard does not mean you absolutely must not use it. Otherwise, how do all C++ programmers work with functions from shared objects with correct ISO C++ code? Just guessing, I might be wrong, I am not very expert in C++.

While experimenting with my code, I found that by sharing a pointer to a struct which contains a reference to the function I want to invoke, my compiler will not complain. I use -Wall and -pedantic while compiling.

My code looks as follows:

myclass.hpp

class myclass
{
    public:
    virtual void dosomething (void)=0;
}

api.hpp

#include <myclass.hpp>
struct API
{
    myclass* (* func)(void);
};

so.hpp

#include <iostream>
#include "myclass.cpp"
#include "api.hpp"

class childclass : public myclass
{
    void dosomething (void)
    {
        std::cout << "Did it.\n";
    }
}

/* function to return a new instance of childclass */

extern "C" myclass* make (void)
{
    return new childclass;
}

/* struct that contains a pointer to the function */

extern "C" API interface;
API interface
{
    make
};

host.cpp

#include <iostream>
#include <dlfcn.h>
#include "myclass.hpp"
#include "api.hpp"
int main (void)
{
    void * th = dlopen("./so.so", RTLD_LAZY);
    /* error checking was here */

    #ifndef usefunction

        API* api = static_cast<API*>( dlsym(th, "interface") );
        myclass * inst = api->make();
        inst->dosomething();

    #else

        myclass* (*func)(void) = reinterpret_cast<myclass* (*)(void)>( dlsym(th, "make") );
        /* will never get to this point */

    #endif

    return 0;
}

Having already compiled so.so, I then compile my host.cpp file.

g++ -ldl -Wall -pedantic host.cpp -o host
Compiles fine, program correcly prints Did it. when run.

g++ -ldl -Wall -pedantic host.cpp -o host -Dusefunction
Complains

In function ‘int main(int, char**)’:
warning: ISO C++ forbids casting between pointer-to-function
and pointer-to-object [enabled by default]

I know it is just a warning, but why is not the warning print in the first case, when using the struct, if in the end I am indirectly being able to reference a pointer to a function that resides in a shared object?

Speaking of which, anybody knows a way to achieve all this in a totally correct ISO C++ manner? Does it even exist?

  • 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-12T00:24:01+00:00Added an answer on June 12, 2026 at 12:24 am

    If you have your function pointer wrapped in a struct, then you are getting around the problem by adding an extra level of indirection. Imagine if a function pointer took 10 bytes, but an object pointer took four bytes. Your struct would be at least 10 bytes, and you would have a 4-byte pointer to that. This is all perfectly fine. When you access the struct to get to your function pointer, you pull out the full 10 bytes. Nothing is lost. However, if you were to cast the 10-byte function pointer to a 4-byte object pointer, you would necessarily lose 6 bytes of information.

    This isn’t an actual problem here, since platforms that support dlsym must have void pointers that are large enough to store the address of a function pointer, but the compiler is trying to prevent you from writing non-portable code.

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

Sidebar

Related Questions

Yesterday, I posted a question on some tips doing this Remote Client-Server Application in
I'm teaching myself CIL and have been doing ok so far (just really started
I was doing some things with a viewport yesterday in XNA, and couldn't figure
Greetings I started developing with WPF yesterday and have ran into some issues. I
I have been searching this since yesterday and still nothing. From all that researching
I was working with quality yesterday doing some formal testing. In their procedure they
I've been doing some WPF development for the last few months, and I've recently
I've been doing some funny stuff in personal projects with Qt. I'm not an
I was propted by apple to upgrade my debugger settings yesterday, In doing so
Yesterday I've posted question about SQLite performance issues ( Terrible performance in Zend Db

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.