C和Fortran互相传递动态数组
C和Fortran的相互调用传递数值的方法有很多,但是F03标准的出笼,使用ISO_C_BINDING进行C和Fortran的互相调用有着更显著的优势:
1、与编译器和平台无关;
2、Fortran中可以定义C的数据类型;
3、使用Interface接口声明,清晰明了;
这里主要介绍C传递动态数组给Fortran的一种解决思路。
C代码:
1 #include <stdlib.h> /* malloc */
2 #include <stdio.h> /* printf */
3 struct my_struct{
4 int num; /* length of array*/
5 int *array; /* dynamic array*/
6 }my_struct;
7 int j=12;
8 struct my_struct make_array(){
9 struct my_struct tmp;
10 int i;
11 tmp.num = j;
12 /* initialize array */
13 tmp.array = (int*)malloc(tmp.num*sizeof(int));
14 for(i=0;i<tmp.num;i++)tmp.array[i]=(i+1)*(i+1);
15 j+=3;
16 return tmp;
17 }
Fortran代码:
program f_call_c
use,intrinsic::iso_c_binding
implicit none
! define same struct in C
type,bind(c)::my_struct
integer(c_int)::nn
type(c_ptr)::array
end type
interface
type(my_struct) function make_array() bind(c,name='make_array')
import !! Make iso_c_binding and my_struct visible here
end function
endinterface
integer(C_INT), pointer :: array(:) => NULL()
type(my_struct)::xyz
integer::j
do j=1,3
xyz = make_array()
call c_f_pointer(xyz%array,array,[xyz%nn])
write(*,*)xyz%nn
write(*,*)array
enddo
end program
==========================================================
2014-03-14 更新 C传递动态数组到Fortran的代码
2014-03-17 补充Fortran传递动态数组到C的代码
1、根据gfortran的使用手册:If a pointer is a dummy-argument of an interoperable procedure, it usually has to be declared using the VALUE attribute. void* matches TYPE(C_PTR), VALUE, while TYPE(C_PTR) alone matches void**.
2、增加的代码在C中定义全局指针用于分配内存开辟动态数组,然后Fortran调用,最后C代码中释放内存
3、gfortran可以直接同时编译c和fortran文件,gcc同样亦可不过需要添加编译参数
小结:推荐使用Function而不是subroutine定义C的interface,分配给动态数组的内存要清空,避免内存泄漏,使用指针的话用完后记得指向空指针。
************************************
C传递动态数组到Fortran
C pass dynamic array to Fortran
************************************
C代码:
#include <stdlib.h>
#include <stdio.h>
// define a struct
- struct conn{
int ind;
float *list;
int length;
};
// define a pointer struct
struct conn *abc=NULL;
// length of dynamic array
int array_len=0,struct_len=0;
// define a pointer to pointer
float *vector2=NULL;
/* float array printing function */
void print_float_array(float *array, int len)
- {
int i;
for(i=0; i<len; i++)
printf("%f | ", array[i]);
putchar('\n');
}
void clear_array(void)
- {
- if(vector2!=NULL){
free(vector2);
vector2=NULL;
printf("Clear pointer successfully\n");
}
- else{
printf("Already NULL!\n");
}
}
// Method 1
void dynamic_array(int n1,float **a,int *n2)
- {
int i;
if(vector2!=NULL)clear_array();
// Allocate array
vector2 = (float*)malloc(n1*2*sizeof(float));
// Set values
for(i=0;i<n1*2;i++){vector2[i] = (float)i*i+1.0;}
// display array
print_float_array(vector2,n1*2);
//
*a = vector2;
*n2 = 2*n1;
// Set length of array
array_len = n1*2;
}
// Method 2
float* d_array(int n1,int *n2)
- {
int i;
if(vector2!=NULL)clear_array();
// Allocate array
vector2 = (float*)malloc(n1*2*sizeof(float));
// Set values
for(i=0;i<n1*2;i++){vector2[i] = (float)i*i+1.0;}
print_float_array(vector2,n1*2);
*n2 = 2*n1;
array_len = n1*2;
return vector2;
}
void clear_struct(void)
- {
int i;
- if(abc!=NULL){
- for(i=0;i<struct_len;i++){
free(abc[i].list);
abc[i].list=NULL;
}
struct_len = 0;
free(abc);
abc = NULL;
printf("Clear struct array successfully!");
putchar('\n');
}
- else{
printf("Already NULL\n");
}
}
// Pass dynamic struct array
struct conn *d_struct(int n1,int *n2)
- {
int i,j;
if(abc!=NULL)clear_struct();
// Allocate sturct array
abc = (struct conn*)malloc((n1+1)*sizeof(struct conn));
// Set values
- for(i=0;i<n1+1;i++){
abc[i].list = (float*)malloc((i+2)*sizeof(float));
abc[i].ind = i+1;
abc[i].length = i+2;
- for(j=0;j<i+2;j++){
abc[i].list[j] = (float)j*j+0.5;
}
}
*n2 = n1 + 1;
struct_len = n1 + 1;
return abc;
}
Fortran代码:
- module dynamic
use,intrinsic::iso_c_binding
implicit none
! Define struct
- type,bind(c)::connect
integer(c_int)::ind
type(c_ptr)::list
integer(c_int)::length
end type
- interface
! Method 1 via subroutine
- subroutine dynamic_array(length,a,n)bind(c,name='dynamic_array')
import
implicit none
integer(c_int),intent(in),value::length
integer(c_int),intent(out)::n
type(c_ptr),intent(out)::a
end subroutine
! Mehtod 2 via function
- type(c_ptr) function dynamic_array2(length,n)bind(c,name='d_array')
import
implicit none
integer(c_int),intent(in),value::length
integer(c_int),intent(out)::n
end function
! Pass dynamic struct array via function
- type(c_ptr) function dynamic_struct(length,n)bind(c,name='d_struct')
import
implicit none
integer(c_int),intent(in),value::length
integer(c_int),intent(out)::n
end function
! Clear struct array
- subroutine clear_struct()bind(c,name='clear_struct')
import
implicit none
end subroutine
! Clear array
- subroutine clear_array()bind(c,name='clear_array')
import
implicit none
end subroutine
end interface
end module
- program test
use dynamic
implicit none
real,pointer::array(:)
integer,pointer::nn
type(c_ptr)::c_array,c_struct
type(connect),pointer::conn(:)
integer::i,j,k
nullify(array)
nn=>NULL()
c_array=c_null_ptr
i = 10
j = 0
call dynamic_array(i,c_array,j)
!c_array = dynamic_array2(i,j)
write(*,*)i,j
call c_f_pointer(c_array,array,[j])
write(*,*)
write(*,*)array
c_array=c_null_ptr
c_struct = c_null_ptr
array=>null()
i = 2*2
j = 0
c_struct = dynamic_struct(i,j)
write(*,*)i,j
call c_f_pointer(c_struct,conn,[j])
- do i=1,j
array=>null()
call c_f_pointer(conn(i)%list,array,[conn(i)%length])
write(*,*)"Index:",i
write(*,*)"List:",(array(k),k=1,size(array))
enddo
call clear_struct()
call clear_array()
end program
************************************
Fortran传递动态数组到C
Fortran pass dynamic array to C
***********************************
Fortran代码:
- module f2c_dynamic
use,intrinsic::iso_c_binding
implicit none
integer(c_int),allocatable,target::ind(:)
contains
- type(c_ptr) function init_array(n1,n2)bind(c,name="allocArray")
implicit none
integer(c_int),intent(in),value::n1
integer(c_int),intent(out)::n2
integer::i
write(*,*)"************************"
write(*,*)"Fortran"
write(*,*)"************************"
write(*,*)"input:",n1,n2
call clear_array()
init_array = c_null_ptr
allocate(ind(n1+6))
- do i=1,n1+6
ind(i) = i**2+2
write(*,*)"index:",i," value:",ind(i)
enddo
n2 = n1 + 6
write(*,*)"array size:",n2
init_array = c_loc(ind(1))
write(*,*)"Return pointer"
return
end function
- subroutine init_array2(n1,n2,a)bind(c,name="allocArray2")
implicit none
integer(c_int),intent(in),value::n1
integer(c_int),intent(out)::n2
type(c_ptr),intent(out)::a
integer::i
write(*,*)"************************"
write(*,*)"Fortran"
write(*,*)"************************"
write(*,*)"input:",n1,n2
call clear_array()
a = c_null_ptr
allocate(ind(n1+2))
- do i=1,n1+2
ind(i) = i**3-1
write(*,*)"index:",i," value:",ind(i)
enddo
n2 = n1 + 2
write(*,*)"array size:",n2
a = c_loc(ind(1))
write(*,*)"Return pointer"
return
end subroutine
- subroutine clear_array()bind(c,name="clearArray")
implicit none
if(allocated(ind))then
deallocate(ind)
write(*,*)"Clear successfully"
endif
end subroutine
end module f2c_dynamic
C代码:
#include <stdlib.h> /* malloc */
#include <stdio.h> /* printf */
int *allocArray(int ,int *);// via fortran function
void allocArray2(int,int *,int **);// via fortran subroutine
void clearArray();
/* float array printing function */
void print_int_array(int *array, int len)
- {
int i;
for(i=0; i<len; i++)
printf("%d | ", array[i]);
putchar('\n');
}
int main(int argc,char *argv[])
- {
int n1=5;
int n2=0;
int *a=NULL;
int *b=NULL;
a=allocArray(n1,&n2);
printf("********************\nC output\ninput:%d size:%d\n",n1,n2);
print_int_array(a,n2);
clearArray();
n2 = 0;
n1 = 6;
allocArray2(n1,&n2,&b);
printf("********************\nC output\ninput:%d size:%d\n",n1,n2);
print_int_array(b,n2);
clearArray();
a=NULL;
b=NULL;
return 0;
}
C和Fortran互相传递动态数组的更多相关文章
- ALLOCATE语句分配FORTRAN动态数组方法(转自http://blog.csdn.net/zhuxianjianqi/article/details/8067174)
数组的动态分配 a) 可分配数组 数组可以是静态的也可以是动态的.如果数组是静态的,则在编译时就被分配了固定的储存空间,并且直到程序退出时才被释放.程序运行时静态数组的大小不能改变.静态数组的缺 ...
- fortran常用语句--读写带注释文档、动态数组等语法
1.判断读取文档有多少行数据(文档最后的空行不计入其中): 首先在变量定义区域下方和执行语句前声明在程序中要被调用的GetFileN函数: external GetFileN 接下来在函数外部后边写上 ...
- C++中关于[]静态数组和new分配的动态数组的区别分析
这篇文章主要介绍了C++中关于[]静态数组和new分配的动态数组的区别分析,很重要的概念,需要的朋友可以参考下 本文以实例分析了C++语言中关于[]静态数组和new分配的动态数组的区别,可以帮助大家加 ...
- Delphi 的动态数组
传统的Pascal 语言其数组大小是预先确定的,当你用数组结构声明数据类型时,你必须指定数组元素的个数.专业程序员也许知道些许动态数组的实现技术,一般是采用指针,用手工分配并释放所需的内存. Delp ...
- shared_ptr和动态数组
std::shared_ptr智能指针是c++11一个相当重要的特性,可以极大地将开发者从资源申请/释放的繁重劳动中解放出来. 然而直到c++17前std::shared_ptr都有一个严重的限制,那 ...
- vc++基础班[28]---动态数组及动态链表的讲解
C++中也有相应的动态数组.动态链表.映射表的模板类,就是STL中的:vector.list.map 他们属于C++标准中的一部分,对于程序的移植性来说也是不错的,但是在MFC编程中使用 CArray ...
- [UE4]动态数组:TArray容器
为什么使用UE4提供的容器类? 如果你用过C++的STL库,你就知道STL提供了各种各样的容器/数据结构,使得你对处理很多数据的时候非常快捷高效.UE4同样也提供了类似的库,库里面的类型是以T开头的, ...
- [UE4]C 语言动态数组
在实际的编程中,往往会发生这种情况,即所需的内存空间取决于实际输入的数据,而无法预先确定.对于这种问题,用静态数组的办法很难解决.为了解决上述问题,C语言提供了一些内存管理函数,这些内存管理函数结合指 ...
- Java程序猿学习C++之数组和动态数组
数组: #include <iostream> using namespace std; //模板函数 template <class T> void dump(T val) ...
随机推荐
- mysql 报错Authentication method 'caching_sha2_password' is not supported
原文地址:https://blog.csdn.net/u011583336/article/details/80999043 之前工作中用的数据库多是ms sqlserver,偶尔用到mysql都是运 ...
- linux php安装ODBC扩展
进入php源码安装目录的ext/pdo_odbc $ sudo /data/apps/php/bin/phpize $ ./configure --with-php-config=/data/apps ...
- python实现快速排序、冒泡
快速排序:首先任意选取一个数据(通常选用数组的第一个参数)作为关键数据,然后将比它小的数放在它前面,比它大的数放在后面,这个过程称之为快速排序 def quick_sort(l): if len(l) ...
- 2016年工作中遇到的问题41-50:Dubbo注册中心奇葩问题,wifi热点坑了
41.获得JSON中的变量.//显示json串中的某个变量,name是变量名function json(json,name){ var jsonObj = eval(json); return jso ...
- Java 关于循环的练习--和为n的正数序列
要求:输入一个正数n,输出所有和为n的连续正数序列. 分析可以从1开始连续加,若到i的和等于n则输出1到i之间的连续正数,若到i的和大于n,则改为从2开始连续加,再判断到i的和是否等于n,等于则输出2 ...
- hdu 4422
#include<stdio.h> #include<string.h> #define inf 0x7fffffff int main() { int i,j,k, ...
- bzoj 3786 星系探索 dfs+splay
[BZOJ3786]星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球 ...
- android去除标题栏和状态栏(全屏)
转--http://www.eoeandroid.com/thread-66555-1-1.html 在开发中我们经常需要把我们的应用设置为全屏,这里我所知道的有俩中方法,一中是在代码中设置,另一种方 ...
- 45个android实例源码分享
分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码 http://www.apkbus.c ...
- vs2010 静态使用 opencv 2.46 库
下载opencv2.46的库,假设解压到OpenCV246,设置如下: 在工程的c++的include目录下添加:OpenCV246\opencv\build\include 在工程的c++的lib目 ...