Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Arrays of derived types

kolber__Michael
New Contributor I
450 Views

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
FortranFan
Honored Contributor II
450 Views

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.

Chaudhary__Krishna
450 Views

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.

David_Billinghurst
New Contributor II
450 Views

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

 

Reply