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:
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
I would like to know how I can accomplish this so that I don't have to allocate more space than is required.
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.
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.
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