Friday, 15 February 2013

fortran - What happens with allocatable components of derived types when i use automatic reallocation? -



fortran - What happens with allocatable components of derived types when i use automatic reallocation? -

in program, want create container type, containing array of derived type. want add together type bound procedures container, invoke procedures on components of array. since size of array varies, tried utilize automatic reallocation feature. ran troubles allocatable characters.

here little snippet, showing setup:

module realloc_test implicit none type :: number_t character(:), allocatable :: number_c ! not work ! character(len=10) :: number_c ! works integer :: number_i end type number_t type number_container integer :: listsize type(number_t), allocatable, dimension(:) :: all_numbers contains procedure add together end type number_container contains subroutine add together (this, number_in) class(number_container), intent(inout) :: type(number_t), intent(inout) :: number_in if (.not. allocated(this%all_numbers)) allocate(this%all_numbers(1), source = number_in) this%listsize = 1 else ! reallocate -> add together entry this%all_numbers = [ this%all_numbers, number_in ] this%listsize = size (this%all_numbers) end if end subroutine add together end module realloc_test programme testprog utilize realloc_test implicit none integer :: type(number_t) :: one, two, three, 4 type(number_container) :: number_list 1 = number_t ('one', 1) 2 = number_t ('two', 2) 3 = number_t ('three', 3) 4 = number_t ('four', 4) phone call number_list%add(one) phone call number_list%add(two) phone call number_list%add(three) phone call number_list%add(four) = 1, number_list%listsize print*, number_list%all_numbers(i)%number_c print*, number_list%all_numbers(i)%number_i end end programme testprog

i compiled ifort, using

-assume realloc_lhs

to enable automatic reallocation. output reads:

1 ??n 2 3 4 4

the lastly entry displayed correctly. copying old part of array seems cause problems allocatable component. when utilize fixed character length, don't run trouble. happens, when automatic reallocation used way? there other options more safely?

i think seeing consequences of compiler bug described @ https://software.intel.com/en-us/articles/incorrect-results-assigning-array-constructor-with-allocatable-components . bug still in ifort 15.0.0.

the workaround shown in link utilize temporary build array ahead of final assignment allocatable component.

fortran fortran2003

No comments:

Post a Comment