首页 文章

当我使用自动重新分配时,派生类型的可分配组件会发生什么?

提问于
浏览
2

在我的程序中,我想创建一个容器类型,包含某个派生类型的数组 . 我想为容器添加类型绑定过程,它调用数组的所有组件上的过程 . 由于数组的大小不同,我尝试使用自动重新分配功能 . 我遇到了可分配字符的问题 .

这是一个小片段,显示设置:

module realloc_test                                                                                                                                                                                                
  implicit none                                                                                                                                                                                                    

  type :: number_t                                                                                                                                                                                                 
    character(:), allocatable :: number_c    ! this does not work                                                                                                                                                                              
!    character(len=10)         :: number_c   ! this works
    integer                   :: number_i                                                                                                                                                                                            
  end type number_t                                                                                                                                                                                                

  type number_container                                                                                                                                                                                            
    integer :: listsize                                                                                                                                                                                            
    type(number_t), allocatable, dimension(:) :: all_numbers                                                                                                                                                       
  contains                                                                                                                                                                                                         
    procedure add                                                                                                                                                                                                  
  end type number_container                                                                                                                                                                                        

contains                                                                                                                                                                                                           

  subroutine add (this, number_in)                                                                                                                                                                                 
    class(number_container), intent(inout) :: this                                                                                                                                                                 
    type(number_t), intent(inout) :: number_in                                                                                                                                                                     

    if (.not. allocated(this%all_numbers)) then                                                                                                                                                                    
      allocate(this%all_numbers(1), source = number_in)                                                                                                                                                            
      this%listsize = 1                                                                                                                                                                                            
    else  
      ! reallocate -> add entry                                                                                                                                                                                                         
      this%all_numbers = [ this%all_numbers, number_in ]                                                                                                                                                           
      this%listsize = SIZE (this%all_numbers)                                                                                                                                                                      
    end if                                                                                                                                                                                                         
  end subroutine add                                                                                                                                                                                               

end module realloc_test                                                                                                                                                                                            

program testprog                                                                                                                                                                                                   
  use realloc_test                                                                                                                                                                                                 
  implicit none                                                                                                                                                                                                    

  integer :: i                                                                                                                                                                                                     
  type(number_t) :: one, two, three, four                                                                                                                                                                          
  type(number_container) :: number_list                                                                                                                                                                            

  one = number_t ('one', 1)                                                                                                                                                                                        
  two = number_t ('two', 2)                                                                                                                                                                                        
  three = number_t ('three', 3)                                                                                                                                                                                    
  four = number_t ('four', 4)                                                                                                                                                                                      

  call number_list%add(one)                                                                                                                                                                                        
  call number_list%add(two)                                                                                                                                                                                        
  call number_list%add(three)                                                                                                                                                                                      
  call number_list%add(four)                                                                                                                                                                                       

  do i = 1, number_list%listsize                                                                                                                                                                                   
    print*, number_list%all_numbers(i)%number_c                                                                                                                                                                    
    print*, number_list%all_numbers(i)%number_i                                                                                                                                                                    
  end do                                                                                                                                                                                                           
end program testprog

我用ifort编译,使用

-assume realloc_lhs

启用自动重新分配 . 输出如下:

1
 ??n
           2

           3
 four
           4

最后一个条目正确显示 . 复制数组的旧部分似乎会导致可分配组件出现问题 . 当我使用固定字符长度时,我不会遇到麻烦 . 当以这种方式使用自动重新分配时到底发生了什么?还有其他选择可以更安全地完成这项工作吗?

1 回答

相关问题