将fortran的可分配数组传递给C和malloc [英] Passing allocatable array from fortran to C and malloc it

查看:321
本文介绍了将fortran的可分配数组传递给C和malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些Fortran库代码转换为C,以便我们最终拥有Fo​​rtran包装器的C函数。我们使用Fortran 95.



处理可分配数组的首选方法是什么?


  1. 我可以将可分配数组传递给C函数,并将它放到C函数中吗?
    (C函数知道大小被malloc化了)


  2. 在fortran程序中,我可以释放C中的某些东西吗?功能?
    最后,客户端Fortran应用程序释放数组,或者需要调用一个释放内存的C函数。




  3. 一个很小的例子或者一个链接将会被高度赞赏。

    解决方案

    在Fortran 95中,传递可分配数组作为可分配事物,包括Fortran过程。



    在Fortran 2003中,C函数可以为数组存储malloc,然后将其返回给Fortran方面作为来自ISO_C_BINDING内在模块的C_PTR。然后可以使用Fortran POINTER和ISO_C_BINDING模块中的C_F_POINTER过程访问C_PTR指向的存储。



    要释放数组的存储空间,Fortran端将再次调用一个C程序,传递C_PTR,C函数随后将在调用中使用以释放它。

      #include stdlib.h
    int * create_storage()
    {
    / *由四个整数组成的数组。 * /
    return malloc(sizeof(int)* 4);
    }

    void destroy_storage(int * ptr)
    {
    free(ptr);
    }


    程序fortran_side
    USE,INTRINSIC :: ISO_C_BINDING,仅:C_PTR,C_F_POINTER,C_INT
    隐式无
    INTERFACE
    FUNCTION create_storage()BIND(C,NAME ='create_storage')
    USE,INTRINSIC :: ISO_C_BINDING,仅:C_PTR
    IMPLICIT NONE
    TYPE(C_PTR):: create_storage
    END FUNCTION create_storage $ b $ SUBROUTINE destroy_storage(p)BIND(C,NAME ='destroy_storage')
    USE,INTRINSIC :: ISO_C_BINDING,仅:C_PTR
    IMPLICIT NONE
    TYPE(C_PTR ),INTENT(IN),VALUE :: p
    END SUBROUTINE destroy_storage
    END INTERFACE
    TYPE(C_PTR):: p
    INTEGER(C_INT),POINTER :: array(: )
    !****
    p = create_storage()
    CALL C_F_POINTER(p,array,[4])! 4是数组大小。
    !使用数组...
    CALL destroy_storage(p)
    END PROGRAM fortran_side

    在Fortran 201X中,可以提供C头文件和函数以允许C直接与Fortran可分配变量一起工作。


    I am converting some of our Fortran library code into C so that we finally have C functions with Fortran wrappers around it. We are using Fortran 95.

    What is the preferred way to handle allocatable arrays?

    1. Can I pass an allocatable array to a C function and malloc it inside the C function? (The C function knows the size to be malloc'ed)

    2. In the fortran program, can I deallocate something that was malloced in a C function? So finally either the client fortran application deallocates the array or is required to call a C function that frees the memory.

    A small example or a link to one would be highly appreciated.

    解决方案

    In Fortran 95 you can't "pass" allocatable arrays as an allocatable thing to anything, including Fortran procedures.

    In Fortran 2003, the C function can malloc storage for the array, and then return that to the Fortran side as a C_PTR from the ISO_C_BINDING intrinsic module. The storage pointed to by the C_PTR can then be accessed using a Fortran POINTER and the C_F_POINTER procedure from the ISO_C_BINDING module.

    To free the storage for the array, the Fortran side would again call into a C procedure, passing the C_PTR, which the C function then uses in a call to free.

    #include "stdlib.h"
    int *create_storage()
    {
       /* Array of four integers. */
       return malloc(sizeof(int) * 4);
    }
    
    void destroy_storage(int *ptr)
    {
       free(ptr);
    }
    
    
    PROGRAM fortran_side
      USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_F_POINTER, C_INT
      IMPLICIT NONE
      INTERFACE
        FUNCTION create_storage() BIND(C, NAME='create_storage')
          USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
          IMPLICIT NONE
          TYPE(C_PTR) :: create_storage
        END FUNCTION create_storage
        SUBROUTINE destroy_storage(p) BIND(C, NAME='destroy_storage')
          USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
          IMPLICIT NONE
          TYPE(C_PTR), INTENT(IN), VALUE :: p
        END SUBROUTINE destroy_storage
      END INTERFACE
      TYPE(C_PTR) :: p
      INTEGER(C_INT), POINTER :: array(:)
      !****
      p = create_storage()
      CALL C_F_POINTER(p, array, [4])   ! 4 is the array size.
      ! Work with array...
      CALL destroy_storage(p)
    END PROGRAM fortran_side
    

    In Fortran 201X, C header files and functions may be provided to allow C to work directly with Fortran allocatable variables.

    这篇关于将fortran的可分配数组传递给C和malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆