- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page