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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:10:28+00:00 2026-05-13T11:10:28+00:00

Is there any tips one can give me about passing pointers to structs, doubles,

  • 0

Is there any tips one can give me about passing pointers to structs, doubles, functions, … from a C program to a C++ library and back?

  • 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-05-13T11:10:28+00:00Added an answer on May 13, 2026 at 11:10 am

    Assuming you’re coding these in two different libraries static or dynamic (DLLs on windows shared libraries on Linux and other *nix variants) The biggest concerns I have are as follows:

    1. They are compiled with the same compiler. While this isn’t necessary if all C++ exports are exported with a C-style naming convention it is necessary for C++ to C++ calls to class instances between the two C++ modules. This is necessary due to how different compilers mangle C++ exports differently.

    2. Do not cast a C++ class as a C struct. They aren’t the same under the covers, even if the layout of fields are the same. C++ classes have a “v-table” if they have any virtual members; this v-table allows the proper calling of inherited or base class methods.

    3. This is true of C to C or C++ to C++ as well as C to C++. Ensure both use the same byte alignment for the output library. You can only determine this by reading your compiler or development environments documentation.

    4. Don’t mix malloc/free with new/delete. More specifically don’t allocate memory with new and free memory with “free” and vice versa. Many compilers and operating systems handle memory management differently between the two.

    5. Passing function pointers: So long as they are exposed to/from C++ as ”extern “C”” this should be fine. (You’ll either need to reference your compilers documentation on how to determine when a header is being compiled as C or C++ to maintain this in one file, or you will need two separate copies of the same function declaration in each project — I recommend the first)

    6. Passing doubles: This is a built-in type in both C and C++ and should be handled the same.

    7. If you must share an instance of a C++ object with a C function, and act on it from within C code, expose a set of C-exported helper functions which call the appropriate methods on the C++ object. Pure C code cannot properly call methods on C++ objects.

    
        Pseudocode-ish Example:
        // C++ class
        class foo {
           public:
               void DoIt();
        };
    
        // export helper declarations
        extern "C" void call_doit(foo* pFoo);
        extern "C" foo* allocate_foo();
        extern "C" deallocate_foo(foo* pFoo);
    
    
        // implementation
        void call_doit(foo* pFoo)
        {
            pFoo->DoIt();
        }
    
        foo* allocate_foo()
        {
            return new foo();
        }
    
        deallocate_foo(foo* pFoo)
        {
           delete pFoo;
        }
    
        // c consumer
        void main()
        {
            foo* pFoo= allocate_foo();
            call_doit(pFoo);
            dealocate_foo(pFoo);
        }
    
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 399k
  • Answers 399k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's the null-safe dereferencing operator (?.) in Groovy... I think… May 15, 2026 at 4:00 am
  • Editorial Team
    Editorial Team added an answer If you use Javascript, you can do like this: <script… May 15, 2026 at 4:00 am
  • Editorial Team
    Editorial Team added an answer I recommend to configure appenders with a configuration file and… May 15, 2026 at 4:00 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.