分配内存的下一个Fortran数组 [英] Allocating memory in C for a fortran array

查看:154
本文介绍了分配内存的下一个Fortran数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来Fortran编程..我有声明为一个3维数组如下

I am new to fortran programming.. I have a 3 dimensional array declared as follows

REAL*4, DIMENSION(:,:,:), ALLOCATABLE :: a1

我想通过参照数组传递给C或C ++函数用C,而不是在FORTRAN分配内存。它是可能的,或者我是错在FORTRAN理解阵列概念?

I want to pass the array by reference to a C or C++ function and allocate memory in C rather than in fortran. Is it possible or I am wrong in understanding the array concept in fortran ?

推荐答案

这要看,但可能不会,这取决于你的情况。绝对不是一个可移植的方式,如果仅限于Fortran 90的按问题标记。

It depends, but probably not, depending on your circumstances. Definitely not in a portable way, if you are limited to Fortran 90 as per the question tag.

请注意,您的Fortran语言变量是一个分配的。

Note that your Fortran variable is an ALLOCATABLE.

按照您的要求,它需要一个Fortran编译(和同伴C编译器)实现相对最近出版的技术规范(T229113:2012)。你是在语言发展的最前沿工作 - 和编译器的数量,目前支持该TS很小(如果不是零)。预计这一内容TS将纳入Fortran标准的下一版本,在短短几年时间,再试一次,你可能有更好的运气。

As you've asked, it requires a Fortran compiler (and companion C compiler) that implement the relatively recently published Technical Specification (T229113:2012). You are operating at the bleeding edge of the development of the language - and the number of compilers that currently support this TS is small (if not zero). It is expected that the contents of this TS would be incorporated into next revision of the Fortran standard, so try again in a few years time and you might have better luck.

请注意虽然,TS它限制C能为Fortran语言做分配的方式 - 基本上是C code需用调用Fortran编译提供的例程。您不能使用任何旧分配器(包括malloc的),所以这仍然可能不适合。

Note though, that the TS limits the way in which C can do the allocation for Fortran - basically the C code needs to call a Fortran compiler supplied routine. You can not use any old allocator (including malloc), so this still may not suit.

然而,Fortran 2003的加入可能性的Fortran指针指向的(如你不愿allocatables)的C.做内存分配的C code指针返回到适当大小的一块内存,其中的Fortran程序接收背照式C_PTR的对象。该Fortran code然后调用程序C_F_POINTER到C地址在C_PTR对象与Fortran指针指向的关联。 C_PTR和C_F_POINTER是内在ISO_C_BINDING模块中的实体。

However, Fortran 2003 added the possibility for the memory allocation of Fortran POINTER's (not allocatables as you've asked) to be done in C. The C code returns a pointer to a chunk of memory of the appropriate size, which the Fortran procedure receives back as object of type C_PTR. The Fortran code then calls the procedure C_F_POINTER to associate the C address in the C_PTR object with the Fortran pointer. C_PTR and C_F_POINTER are entities in the intrinsic ISO_C_BINDING module.

粗例如:

USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_F_POINTER
INTERFACE
  FUNCTION get_some_memory() BIND(C, NAME='get_some_memory')
    IMPORT :: C_PTR
    TYPE(C_PTR) :: get_some_memory
  END FUNCTION get_some_memory
END INTERFACE

! REAL*4 isn't Fortran.  Correct syntax below.
REAL(4), POINTER, DIMENSION(:,:,:) :: a1

! get_some_memory would typically be told how much memory was needed.
! n1, n2, n3 are integer with the relevant extents for each dimension.
CALL C_F_POINTER(get_some_memory(), a1, [n1,n2,n3])

! Equivalent call to free required at some later stage.


void *get_some_memory()
{
  return malloc(some_number_of_bytes);   
} 

除此之外,还有依赖于处理器的(即非便携式)的技巧,你可以玩,但他们的标准语言之外。

Beyond this, there are processor dependent (i.e. non-portable) tricks that you can play, but they are outside of the standard language.

这篇关于分配内存的下一个Fortran数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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