Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As 6502 explained, it is not possible to do this in portable C without some kind of context argument being passed to the callback, even if it doesn't name <code>secret_id</code> directly. However, there are libraries such as <a href="http://www.gnu.org/software/libffcall/trampoline.html" rel="nofollow">Bruno Haible's trampoline</a> that enable creation of C functions with additional information (closures) through non-portable means. These libraries do their magic by invoking assembly or compiler extensions, but they are ported to many popular platforms; if they support architectures <em>you</em> care about, they work fine.</p> <p>Taken from <a href="http://www.stackprinter.com/export?service=stackoverflow&amp;question=644246&amp;printer=false&amp;linktohome=true" rel="nofollow">the web</a>, here is an example of code that trampoline enables is this higher-order function that takes parameters <code>a</code>, <code>b</code>, and <code>c</code> (analogous to your <code>secret_id</code>, and returns a function of exactly one parameter <code>x</code> that calculates <code>a*x^2 + b*x + c</code>:</p> <pre><code>#include &lt;trampoline.h&gt; static struct quadratic_saved_args { double a; double b; double c; } *quadratic_saved_args; static double quadratic_helper(double x) { double a, b, c; a = quadratic_saved_args-&gt;a; b = quadratic_saved_args-&gt;b; c = quadratic_saved_args-&gt;c; return a*x*x + b*x + c; } double (*quadratic(double a, double b, double c))(double) { struct quadratic_saved_args *args; args = malloc(sizeof(*args)); args-&gt;a = a; args-&gt;b = b; args-&gt;c = c; return alloc_trampoline(quadratic_helper, &amp;quadratic_saved_args, args); } int main() { double (*f)(double); f = quadratic(1, -79, 1601); printf("%g\n", f(42)); free(trampoline_data(f)); free_trampoline(f); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload