Dynamically creating functions in C

前端 未结 10 1676
-上瘾入骨i
-上瘾入骨i 2020-12-09 10:52

How can I dynamically create a function in C?

I try to summarize my C problem as follows:

  • I have a matrix and I want to be able to use some function

10条回答
  •  无人及你
    2020-12-09 11:12

    Using FFCALL, which handles the platform-specific trickery to make this work:

    #include 
    #include 
    #include 
    
    static double internalDoubleFunction(const double value, ...) {
        return value;
    }
    double (*constDoubleFunction(const double value))() {
        return alloc_callback(&internalDoubleFunction, value);
    }
    
    main() {
        double (*fn)(unsigned int, unsigned int) = constDoubleFunction(5.0);
        printf("%g\n", (*fn)(3, 4));
        free_callback(fn);
        return 0;
    }
    

    (Untested since I don't have FFCALL currently installed, but I remember that it works something like this.)

提交回复
热议问题