0

This is my code:

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

At the end of execution I got this message: Fortran runtime error: Attempting to allocate alredy allocated variable dS_sn

My intention with this code is to create dynamic array dS_sn with first dimension which is function of i_nn_dm_1(k) and i_nn_dm_1(k). In this case I want to create:

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

What is wrong with my code and is there a way for correct array declaration and memory allocation? Is there any memory leak problem with this code?

Yossarian
  • 5,226
  • 1
  • 37
  • 59
  • 1
    You are trying to do something like [this](https://stackoverflow.com/q/18316592/3157076)? – francescalus Oct 25 '17 at 19:34
  • No. It is not the same problem. In a edited version of question you can find what I want to do exactly. –  Oct 25 '17 at 20:05
  • 1
    looks the same to me. You can not direstly make such a ragged array as you want. Study HPM's answer to the linked question and see if it doesn't do what you want. – agentp Oct 25 '17 at 20:38
  • How can I apply the logic from mentioned question? Sugestion for my case? –  Oct 25 '17 at 20:58
  • Looks like a duplicate to me, at least the current version of the question. – Ross Oct 26 '17 at 04:16
  • 1
    The suggestion is to do exactly.the same thing. Define a derived type and place a 1D allocatable array component inside. Then make an array of theae. Just start coding. If you get a problem while implementing this, come and ask. Fortran arrays have always a rectangular shape. You cannot allocate different rows separately. – Vladimir F Героям слава Oct 26 '17 at 06:08
  • It works now afther all. I am not experienced programer in Fortran so I need to ask. Is there any problem now with memory leak? Is this correct way for a memory deallocation in separate module? –  Oct 26 '17 at 10:12
  • Please do not change the code in the question so drastically. If you have a new question, rewrite the whole question accordingly, or better, ask a new question. Note that it is virtually impossible to make a memory leak with anything `allocatable`. – Vladimir F Героям слава Oct 26 '17 at 11:08
  • I made a rollback. if you *really* want to ask about the updated code here (I rather suggest a new question) then please also update the text of the question and the error message. Do not just update the code, the question made no sense after your edit. – Vladimir F Героям слава Oct 26 '17 at 11:13
  • Ok. I will post a new question. –  Oct 26 '17 at 11:17

0 Answers0