Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
8 Views

Arrays of derived types

I have a derived type that is made up of nested derive types.  Each of these types is an array.  The outer structure is a single structure.  Currently the arrayed derived types have fixed pre-assigned  lengths.  I would like to dynamically assign the size of each type based on the number of entries in the file that I am reading.  When I have tried that by doing something like this

 

type(sample_type), dimension(:), allocatable ::  first_type

 

Then in the subroutine that reads the file, do the following:

read n

allocate(first_type(n))

I am getting a stack overflow before I even get to this code.  I never got that when I had first_type defined as follows

type(sample_type)  first_type(500)

I would like to know how I can accomplish this so that I don't have to allocate more space than is required.

 

Thanks

Michael

 

 

 

0 Kudos
3 Replies
Highlighted
Valued Contributor III
8 Views

What you would like is

What you would like is conceptually feasible in Fortran but perhaps with some adjustments in your design.  Your approach with the ALLOCATABLE attribute should work, at least in principle.

But based on what I see, you provide little information with your issue, "I am getting a stack overflow before I even get to this code."

If you can share a complete reproducer of your problem, readers can try to help.

0 Kudos
Highlighted
8 Views

I think you need to Enable

I think you need to Enable F2003 Semantics in the project properties > Configuration Properties > Fortran > Language in order to allow the allocatable arrays for Derived Types.

0 Kudos
Highlighted
New Contributor II
8 Views

We do this our production

We do this our production code.  Certainly works with Intel Fortran 2013.  

This works for me with Intel Fortran 2019 and gfortran 8.3.0

program allocate_dt
  implicit none
  type sample_type; real :: x  = 1.0; integer :: i = 1; end type
  integer n
  type(sample_type), dimension(:), allocatable ::  first_type
  write(*,*) 'Enter array size'
  read(*,*) n
  allocate(first_type(n))
  write(*,*) first_type(n)%i, first_type(n)%x
  read(*,*)
end program allocate_dt

 

0 Kudos