Fortran, subroutine with allocatable, intent(out) array

Click For Summary
SUMMARY

The discussion focuses on the challenges of using allocatable arrays in Fortran subroutines, specifically when passing them from the main program to the subroutine. The user is attempting to allocate a double complex array in the subroutine with the intent(out) attribute, but encounters crashes. Suggestions include using the intent(inout) attribute for the subroutine parameter, although the user reports unsuccessful attempts. The conversation highlights the complexities of dynamic memory management in Fortran 77/90 and the potential use of interface blocks for better handling.

PREREQUISITES
  • Understanding of Fortran 77/90 syntax and structure
  • Knowledge of dynamic memory allocation in Fortran
  • Familiarity with subroutine parameter attributes (e.g., intent(out), intent(inout)
  • Basic concepts of interface blocks in Fortran
NEXT STEPS
  • Research the use of Fortran interface blocks for managing allocatable arrays
  • Learn about the differences between intent(out) and intent(inout) in Fortran subroutines
  • Explore dynamic memory allocation techniques in Fortran, focusing on allocatable arrays
  • Investigate error handling strategies for memory allocation failures in Fortran programs
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with dynamic arrays and subroutine interfaces, as well as anyone looking to enhance their understanding of memory management in Fortran 77/90.

alyflex
Messages
9
Reaction score
0
Hi,

I have a rather large program written in fortran 77/90. In one of the subroutines a lot of local dynamic arrays are declared.

In order to make this program able to calculate more stuff, I need to bring these dynamic arrays back into the main program.

I have tried to define the arrays allocatable in the main program and then passing them on to the subroutine which sets them as allocatable. But when I allocate them in the subroutine the program crashes.

I have tried to do it with common blocks, but as far as I can see that's a dead end.

I have considered trying interface blocks, but I am still a bit uncertain as to how they work and if it is possible to do it with those.

Does anyone know a way to do this, I have been stuck with this problem for 2 days now, so any help would be much appreciated.

A short example of what I am trying to do

Main.f90
Program main
double complex, allocatable :: Vlm(:)
...
Call init_potential(input,output,Vlm)
...
End main



init_potential.f90
subroutine init_potential(input,output,Vlm)
double complex, allocatable, intent(out) :: Vlm(:)
...
allocate(Vlm(sizeofVlm))
...
end init_potential
 
Technology news on Phys.org
If you're allocating in the main program, have you tried defining it as INTENT(INOUT) in the subroutine?
 
minger said:
If you're allocating in the main program, have you tried defining it as INTENT(INOUT) in the subroutine?

I'm not allocating it in the main program, but I have tried it anyways, with no succes.
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 59 ·
2
Replies
59
Views
11K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
13K