because I want to provide a perl wrapping for an mpfr function that takes a "pointer to function" as one of its arguments

Usually, the reason for passing a function pointer to another function is so the called function can either call the other function, or set up a call-back so something else (like an event handler) can call it.

That means you need to provide an address that is "local" to the function you are passing the pointer to.

With static linking, your XSubs will be in the same address space as the functions you want pointers to.

With dynamic linking, the addresses your Xsub will have will be "mapped" addresses. That is, addresses in the XSub's address space that it can use to call the functions in the DLL's address space.

I would suggest creating a C array of function pointers initialized to the addresses of the functions you need pointers for:

funcp FuncArray[] = { mpfr_mul, mpfr_read, /* etc */ }

Then your XSub can fetch the needed pointer by index.

To avoid having to figure out the indexes, you can use Perl to read the C source of FuncArray and create a hash with the function names as keys and indexes as values.


In reply to Re: [OT: C] Getting pointers to functions by RonW
in thread [OT: C] Getting pointers to functions by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.