Software Archive
Read-only legacy content
17061 Discussions

ALLOCATABLE User-Defined Types and Strange Behavior

michael-a-carr
Beginner
433 Views
I have something similar to following code:

 
--- type.f90 --- 
MODULE MTYPE 
TYPE NEWTYPE 
  INTEGER, ALLOCATABLE :: N(:) 
END TYPE 
END MODULE 
 
--- subrt.f90 --- 
MODULE MSUBS 
CONTAINS 
SUBROUTINE SETUP(a) 
  TYPE(NEWTYPE), INTENT(OUT) :: a 
  ALLOCATE(a%n(1000)) 
  a%n = 0 
END SUBROUTINE 
END MODULE 
 
--- main.f90 --- 
PROGRAM TEST 
USE MTYPE 
TYPE(NEWTYPE) :: a 
CALL SETUP(a) 
END PROGRAM 


When I run this program, it works fine until I return from the subroutine. When that happens, the array I allocated in the subroutine contains random values. Does the ALLOCATE command have scope in this case? i.e. Do I have to allocate in the outermost program unit or can I allocate in a subroutine like this?

Thank you,
Michael Carr
0 Kudos
1 Reply
Intel_C_Intel
Employee
433 Views
After inserting the line USE MTYPE in module MSUBS and USE MSUBS in program TEST so that the above compiles, it looks like the first few elements of a%n are incorrect. Compaq just put this allocatable components stuff in their compiler and they don't have all the kinks worked out yet. You should send them a bug report when you see misbehavior like this. Probably the above would work if you used the F95-compatible method of a pointer component rather than an allocatable component.
0 Kudos
Reply