Fortran-C函数指针在某些情况下导致C代码出现seg故障

Fortran-C函数指针在某些情况下导致C代码出现seg故障,fortran,intel-fortran,fortran-iso-c-binding,Fortran,Intel Fortran,Fortran Iso C Binding,我正在修改,以便可以传递函数指针而不是函数名。为此,我使用Fortran绑定(c)函数,调用c_funloc,调用c函数并将其分配给派生类型(c_ptr)。然而,C代码根据我如何实现代码给出了错误 C代码: #include <stdio.h> #include <stdlib.h> struct function_struct { double (* function) (double x); }; typedef struct function_struct

我正在修改,以便可以传递函数指针而不是函数名。为此,我使用Fortran绑定(c)函数,调用c_funloc,调用c函数并将其分配给派生类型(c_ptr)。然而,C代码根据我如何实现代码给出了错误

C代码:

#include <stdio.h>
#include <stdlib.h>
struct function_struct 
{
  double (* function) (double x);
};

typedef struct function_struct gsl_function;

gsl_function *function_cinit(double (*func)(double x)) {

    gsl_function *result;

     if (func) {
      printf("Passed Function Not Null\n");
    }

    printf("The size of gsl_function is %zu\n", sizeof(gsl_function));
    result = (gsl_function *) malloc(sizeof(gsl_function));
    result->function = func;

    printf("Res: %f\n", (*func)(2.0));

    if (result) {
      printf("Result Not Null\n");
    }

    return result;
}

找到了答案!事实证明,我不需要改变任何东西来允许代码接受过程指针

!Function passed with procedure pointer
  function fp_init(fun)
    !Changed from:
    !procedure(rhox), pointer :: fun
    procedure(rhox):: fun
    type(c_ptr) :: fp_init
    type(c_funptr) :: fp

    fp = c_funloc(fun)

    call c_f_procpointer(fp, fun)

    fp_init = function_cinit(fp)
  end function fp_init

我从伪参数中删除了“pointer”属性,现在它可以工作了。我不知道为什么。

您使用的是14.0.2吗?我使用的是14.0.1,将在@IANHT进行更新。14.0.2中有一些错误修复,可能与通过值向绑定(C)函数传递指针有关。请看,我不认为
rhox()
的抽象接口是否需要
bind(c)
属性。但如果您感兴趣,请在我的问题中进一步跟进:
!Function passed with procedure pointer
  function fp_init(fun)
    !Changed from:
    !procedure(rhox), pointer :: fun
    procedure(rhox):: fun
    type(c_ptr) :: fp_init
    type(c_funptr) :: fp

    fp = c_funloc(fun)

    call c_f_procpointer(fp, fun)

    fp_init = function_cinit(fp)
  end function fp_init