我有一个模块中的几个数组变量是动态的,稍后分配给模块外的两个子例程之一。但是,在一个子程序中,我希望数组是一维的,而在另一个子程序中,我希望它是二维的。动态数组排名
原则上我希望在模块中这样的事情,但我不相信这是可能的声明区域?:
if (option1) then
real (kind=8), allocatable :: arr1(:)
else
real (kind=8), allocatable :: arr1(:,:)
endif
是否有可能分配申报办法有维是动态的?
编辑1:我这样做的原因是我添加一个新的子程序到现有的代码库,但我想向后兼容。 arr1仅由两个独立的子程序使用,主程序完全不使用它。下面是该理念的一些更完整的代码:
program myprog
use inputs
call read_inputs
if (option1) then
call do1
else
call do2
endif
contains
subroutine read_inputs
use inputs
use mymod
!!!read from file .logical. option1, integers N1, N2
!allocate arrays
if (option1) then
else
endif
end subroutine read_inputs
subroutine do1
use inputs
use mymod
allocate(arr1(N1))
!do stuff with arr1
end subroutine do1
subroutine do2
use inputs
use mymod
allocate(arr1(N1,N2))
!do stuff with arr1
end subroutine do2
end program
module inputs
logical :: option1
integer :: N1, N2
end module inputs
module mymod
use inputs
!!!!can I do something here to make the rank of arr1 dynamic? I don't think the following will work
if (option1)
real (kind=8), allocatable :: arr1(:)
else
real (kind=8), allocatable :: arr1(:,:)
endif
end module mymod
我可能只是在mymod,ARR1两个独立的变量,arr1_new。我只是希望避免这种情况。
您可以使用执行该作业的子例程来重载'allocate'语句。 Fortran 90年龄将近26岁。你使用什么编译器?如果使用现代的Fortran(2003+)编译器,我建议定义派生的数据类型,同时使用2维和3维可分配类型组件以及相应的类型绑定过程。 Fortran 90不允许这样做,相反你必须使用容易发生内存泄漏的指针。 – jlokimlin
我不明白你的问题。数组在主程序或模块中的外观如何?那里是1D还是2D?主程序或模块是否关心子程序如何处理它(1D或2D)?也许你可以使用简单的存储关联,但你应该显示更多的代码。 Ceterum censeo'kind = 8'很丑,臭和邪恶http://stackoverflow.com/a/856243/721644 –
@jlokimlin编译器不是问题,但不幸的是,这是一个包含多个用户的大型代码库,所以对于在可预见的将来,它将不得不成为Fortran90。 – Michael