Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
1 View

Derived types - problem with passing to subroutines

Dear colleagues, I have the following problem with derived type variables.

Dear colleagues, I have the following problem with my program:

I define the following derived type

module mod_my

    implicit none  

    type coeff_data_struct

        complex(kind=DP), allocatable, dimension(:) :: Ex, Ey, Hx, Hy

        real :: f

        integer :: bnd

    end type
 

contains

   subroutine fun1()

        type(coeff_data_struct), allocatable, dimension(:) :: str1

        call fun1(str1)

   end fun1 

   subroutine fun2(str1)

        type(coeff_data_struct), allocatable, dimension(:), intent(inout) :: str1

        call fun2(str1)

   end fun2

   subroutine fun3()

        type(coeff_data_struct), allocatable, dimension(:), intent(inout) :: str1

        allocate(str1(10))

        do n = 1,10,1

             allocate(str1(n) % Ex(10))

             allocate(str1(n) % Ey(10))

             allocate(str1(n) % Hx(10))

             allocate(str1(n) % Hy(10))

             do m = 1,10,1

                   str1(n) % Ex(m) = m

                   str1(n) % Ey(m) = m

                   str1(n) % Hx(m) = m

                   str1(n) % Hy(m) = m

             end do 

         end do

   end fun3 

end module 

and from the main program 

program main()

use mod_my

call fun1()

end 

The problem is when I want to see the values assigned to the allocated arrays of allocated structure using debug mode in fun2() or fun1() I get "undefined pointer/array". My question is how should I pass derived types which are allocated two or three functions below in subroutine calling hierarchy?

P.S. Sorry for any typos but the forum gui is ot very "user-friendly"  

 

 

 

0 Kudos
6 Replies
Highlighted
Black Belt
1 View

Sorry, your code does not

Sorry, your code does not make sense. Subroutine Fun3 is never called. Fun1 and Fun2 recursively call themselves, with no way to end the recursion. Presumably, you meant to make Fun1 and Fun3 take the variable str1 as an argument. Please fix these inconsistencies, and post the corrected code using the {...} button in the toolbar when you post, selecting Fortran as the style.

0 Kudos
Highlighted
Beginner
1 View

Yes you are right, there are

Yes you are right, there are typos you mentioned (my original code is much more complicated and this example I wrote without compiler to not waist to much time). Below is example with mentioned corrections

 

module mod_my

    implicit none  

    type coeff_data_struct

        complex(kind=DP), allocatable, dimension(:) :: Ex, Ey, Hx, Hy

        real :: f

        integer :: bnd

    end type

 

contains

   subroutine fun1()

        type(coeff_data_struct), allocatable, dimension(:) :: str1

        call fun2(str1)

   end fun1 

   subroutine fun2(str1)

        type(coeff_data_struct), allocatable, dimension(:), intent(inout) :: str1

        call fun3(str1)

   end fun2

   subroutine fun3()

        type(coeff_data_struct), allocatable, dimension(:), intent(inout) :: str1

        allocate(str1(10))

        do n = 1,10,1

             allocate(str1(n) % Ex(10))

             allocate(str1(n) % Ey(10))

             allocate(str1(n) % Hx(10))

             allocate(str1(n) % Hy(10))

             do m = 1,10,1

                   str1(n) % Ex(m) = m

                   str1(n) % Ey(m) = m

                   str1(n) % Hx(m) = m

                   str1(n) % Hy(m) = m

             end do 

         end do

   end fun3 

end module 

and from the main program 

program main()

use mod_my

call fun1()

end 

 

0 Kudos
Highlighted
Black Belt
1 View

Not much can be done unless

Not much can be done unless you provide code that can be compiled and linked to produce an EXE. The code in #3 is not compilable.

On top of that, you have to give directions about how to recognize and display the claimed problem.

0 Kudos
Highlighted
1 View

I do not know who's Fortran

I do not know who's Fortran you are using, but the above does not compile. I made a few revisions to your posted code (keeping with what I assume you intended).

NOTE I use the button above the text box labeled

{...}
code

module mod_my
    implicit none
    integer, parameter :: DP = 8
    type coeff_data_struct
        complex(kind=DP), allocatable, dimension(:) :: Ex, Ey, Hx, Hy
        real :: f
        integer :: bnd
    end type

contains
   subroutine fun1()
        type(coeff_data_struct), allocatable, dimension(:) :: str1
        call fun2(str1)
   end subroutine fun1 

   subroutine fun2(str1)
        type(coeff_data_struct), allocatable, dimension(:), intent(inout) :: str1
        call fun3(str1)
   end subroutine fun2

   subroutine fun3(str1)
        type(coeff_data_struct), allocatable, dimension(:), intent(inout) :: str1
        integer :: n, m
        allocate(str1(10))
        do n = 1,10,1
             allocate(str1(n) % Ex(10))
             allocate(str1(n) % Ey(10))
             allocate(str1(n) % Hx(10))
             allocate(str1(n) % Hy(10))
             do m = 1,10,1
                   str1(n) % Ex(m) = m
                   str1(n) % Ey(m) = m
                   str1(n) % Hx(m) = m
                   str1(n) % Hy(m) = m
             end do 
         end do
   end subroutine fun3 
end module 

program main
    use mod_my
    call fun1()
end 

While debugging, starting with break point at "call fun1()" in the program, and using Step Into:

1) str1 is not visible in program main
2) stepping into fun1 (and to 1st statement within fun1), str1 is visible, however it has not yet been allocated as well as not defined
3) Stepping into fun2, to call fun3, str1 is visible, however it has not yet been allocated as well as not defined
4) Stepping into fun3, to allocate, str1 is visible, however it has not yet been allocated as well as not defined
5) Stepping over allocate, str1 is visible, str1 is defined, thus we can now see Ex, Ey, Hx, Hy, however, those arrays are not yet allocated as well as those arrays are not defined.
6) Reaching do m, within fun3, on 1st iteration of do n, str1(1)%Ex, ...Ey, ...Hx, ...Hy are allocated, and now firstly viewable, but are not defined and contain junk (in debug mode this may be initialized to a unique value indicating undefined).
7) only upon completing thye do n loop within fun3 and reaching end subroutine fun3 statement are all the components of str1 allocated and defined (and visible in the debugger). Actually this occurs just after the ...Hy(m) = m on the last iteration of do m on the last iteration of do n.
8) upon return to fun2, str1 is fully allocated, as well as the members of each element, and arrays therein, and thus visible with the debugger
9) upon return to fun1, str1 is fully allocated, as well as the members of each element, and arrays therein, and thus visible with the debugger
10) upon return to program, str1 is now out of scope, and thus not visible.

Jim Dempsey

0 Kudos
Highlighted
Beginner
1 View

Yes, this example code works

Yes, this example code works also in my Fortran.

Later I will come back with the part of my code where I have the problem I mentioned 

If this simple example works, there must be some other problem.

 

0 Kudos
Highlighted
Black Belt
1 View

Quote:Adam K. wrote:

Adam K. wrote:

... my original code is much more complicated and this example I wrote without compiler to not waist to waste too much time). 

Next time, please let the compiler be your confidant before making a bug report.

0 Kudos