我对Fortran颇为新颖,而且我使用的是fortran中构建的lib,lib有很多TYPE数组。 我尝试使用以下方法通过c程序将值分配给lib中的TYPE数组。 我已经建立了一个c-fortran接口,我从sqlite数据库获取值到c中的ac结构数组中。然后将这个结构数组传递给一个fortran子例程,在那里我将它声明为派生类型,匹配TYPE变量的定义声明在lib中。然后我将值从传递的数组复制到在lib中声明的实际TYPE数组,并将它传递给fortran函数。将派生数据类型假定形状数组从一个子例程传递给fortran中的一个函数90
发生了什么是数组中的值从c传递给fortran子例程,我打印它们以检查它们是否在fortran子例程中,但是当数组从子例程传递到函数时,值会出现乱码。我按照假定的形状数组传递数组。该函数在模块内声明,因此我认为调用子程序不需要接口。
我不完全理解发生了什么,我也尝试在TYPE声明中使用序列。 我使用g95,gcc 4.0.3 complilers。 数组中的所有值都是REAL类型(KIND = 8),并且c程序中的等效值是双倍的。
考虑一个在其中声明TYPE(something),TYPE(Something2)的库。我在Fortran子例程中将该库作为模块导入。
让我们假设
TYPE(something_lib) is
REAL(kind =8) ::A
REAL(kind=8) ::B
END TYPE
在lib
TYPE(SOMETHING2_lib) !this is also declare in the lib
!I have a C program in which
! in which
/////////////////////////// ////////////////////////////////////////////////// //////////////
// C program
struct SomethingC
{
double a
double b
} ;
struct SomethingC x[2]
struct something2C s[2] // something similar to the first struct
//i fill the values in x ,s from database in proper format.(doubles).
//i call the fortran subroutine in the c program
A_(x,s); //call to fortran subroutine
///////////////////////////// ////////////////////////////////////////////////// //////////// //在Fortran里子程序
SUBROUTINE A (x,s)
USE Lib_module ! this LIB_Module also contains the function func
TYPE G
REAL(kind =8) ! this is defined similar to TYPE something(in lib) by me
REAL(kind =8)
END TYPE G
TYPE G2
similar to TYPE Something2 in lib
END TYPE G2
TYPE(something_lib) :: D(2) !derived type declared in lib
TYPE(Something2_lib)::E(2) ! derived type declared in lib
TYPE(G)::x(2)
TYPE(G2)::s(2)
! x, s are struct arrays from c which are now declared in the fortran function
copy code for
copying values from
x to D
s to E
print all values of
D
Print all values of
E
!this prints the values correct as expected for both x,d
func(D,E) ! this function is defined in the lib . The function is in the
! LIB_module
! so no interface will be required (i think)
! IN the Function
FUNCTION func(D,E) (while debugging)
TYPE(something_lib) :: INTENT (IN) D(:)
TYPE (something2_lib)::INTENT (IN) E(:)
when i try to print values of D , E in the
function i get garbled values like
1180333333
2.33419537006E-313
!when out of the function and back in the subroutine i.e after the call(while debugging)
! if I print the values of D,E here they print ok
END SUBROUTINE
因此它们在功能通过后,他们越来越乱码,但在 子程序确定。 我的问题是为什么会发生这种情况? 我该如何解决它?
我们并不完全了解你在做什么。在代码中看到问题是非常困难的。 –
在这一点上有效的方法是给我们提供代码,代码的截断版本和相关部分,或者代码的骨架/原理图版本。当然,执行时会产生任何错误。 – MarkWayne
@高性能我会编辑我的问题来提供一个框架。 – codefor