2D动态数组分配

时间:2017-10-25 19:07:46

标签: fortran

这是我的代码:

Program Dynamic_Array

Use Variables
Use Calculation
Use Dealloaction_Module

Implicit none

Call Subroutine_0

Call Subroutine_1

End Program Dynamic_Array

Module Variables

Implicit none

Integer :: i , k
Integer , parameter :: Br_sn_cvo = 12
Integer , parameter :: Br_nn_mre = 3
Integer , parameter :: Br_nn_cvo = 14
Integer , dimension ( Br_nn_mre ) :: i_nn_dm_1 , i_nn_dm_2
Integer , allocatable , dimension( : , : ) :: dS_sn

End Module Variables

Module Calculation

Use Variables

Implicit none

Contains

Subroutine Subroutine_0

Loop_1: Do k = 1, Br_nn_mre

        i_nn_dm_1(k) = Br_sn_cvo + Br_nn_mre + 1 + Br_nn_cv * ( k - 1 )
        i_nn_dm_2(k) = Br_sn_cvo + Br_nn_mre + k * Br_nn_cv    

        allocate ( dS_sn ( i_nn_dm_1(k) : i_nn_dm_2(k) , k ) )

        Loop_2: Do i = i_nn_dm_1(k) , i_nn_dm_2(k)

                   dS_sn(i,k) = i + k

                 End Do Loop_2

        End Do Loop_1

End subroutine Subroutine_0

End Module Calculation

Module Dealloaction_Module

Use Variables

Implicit none

Contains

Subroutine Subroutine_1

  deallocate(dS_sn)

Return
End Subroutine Subroutine_1

End Module Dealloaction_Module

执行结束时,我收到了以下消息: Fortran runtime error: Attempting to allocate alredy allocated variable dS_sn

我对此代码的意图是创建动态数组dS_sn,其第一维是i_nn_dm_1(k)i_nn_dm_1(k)的函数。在这种情况下,我想创建:

k = 1  dS_sn ( 16 : 29 , 1)
k = 2  dS_sn ( 30 : 43 , 2)
k = 3  dS_sn ( 44 : 57 , 3)

我的代码有什么问题,是否有正确的数组声明和内存分配方法? 这段代码有没有内存泄漏问题?

0 个答案:

没有答案