Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28994 Discussions

Dynamic memory allocation -passing the data

jw
Beginner
394 Views

I have a dynamic array declared in a subroutine, which I then need to pass back to the main.

In my subroutine the dynamic array is declared as follows:

SUBROUTINE TEST(MY_LIST)

TYPE(MY_STRUCT) , ALLOCATABLE :: MY_LIST(:)

- How can I pass the arrary back to the main?

- How should my array be declared in the main routine?

- Do I need deallocate the memory? If yes, then how?

0 Kudos
2 Replies
TimP
Honored Contributor III
394 Views
Allocatable is deallocated automatically when the subroutine where it is declared goes out of scope. Unfortunately, that means it isn't safely available to the caller. Several solutions are available, such as declaring it in a module which you USE in main and in the subroutine. You could DEALLOCATE explicitly when you are done with it.
0 Kudos
IanH
Honored Contributor III
394 Views

tim18 has given the F95 answer, if you can use F2003, then you need to have an explicit interface for your subroutine visible to the caller (perhaps because the subroutine is in a module) and you need to declare both the actual and dummy argument with the allocatable attribute and with the same rank (using (:,...)). Here's an example that also tries to demonstrate the impact of INTENT. Move the CALLs around to cover all options...

(Edit: I botched the paste-code thing - trying as an attachment)

0 Kudos
Reply