Fortran Coder

标题: Type 包含 allocatable 数组 [打印本页]

作者: 愤怒的三炮    时间: 2023-3-25 02:04
标题: Type 包含 allocatable 数组
可分配数组中的元素是type类型,而元素中又包含可分配数组,该怎么解决呢?
[Fortran] 纯文本查看 复制代码
    type mytype
        sequence
        integer,dimension(:),allocatable:: array_type
        integer,dimension(10):: other_variables
    end type
   
    type(mytype),dimension(:),allocatable:: array_main
   
    allocate(array_main%array_type(10))
   
    allocate(array_type(10))

    end

提示错误:
Component to the right of a part reference with nonzero rank must not have the ALLOCATABLE attribute


作者: 唐汉    时间: 2023-3-25 06:46
[Fortran] 纯文本查看 复制代码
program main

   implicit none

   type :: mytype
      sequence
      integer, dimension(:), allocatable :: array_type
      integer, dimension(10) :: other_variables
   end type mytype

   type(mytype), dimension(:), allocatable :: array_main
   integer :: i

   allocate (mytype :: array_main(10))
   do i = 1, size(array_main)
      allocate (array_main(i)%array_type(10))
   end do

end program main


不晓得是不是正确理解了你的问题,但是应该只是格式问题。
作者: kyra    时间: 2023-3-25 08:00
根据我的习惯,做了一些修改。

我的有些习惯不是必须的,但我个人认为会让更清晰。

[Fortran] 纯文本查看 复制代码
type T_mytype !类型名做个区分
  sequence
  integer,allocatable:: array_type(:) !非必要不写dimension
  integer:: other_variables(10)
end type T_mytype
!必要写 dimension 的情况是,需要大量定义相同大小尺寸的多个数组
type(T_mytype),allocatable:: array_main(:)
integer :: j
allocate(array_main(3)) !先分配array_main
do j = 1 , size(array_main)
  allocate(array_main(j)%array_type(10)) !再逐个分配array_type
end do
!allocate(array_type(10)) !这句多余,且无意义

end

作者: weixing1531    时间: 2023-3-25 12:08
二维不等长数组的固定套路
作者: 愤怒的三炮    时间: 2023-3-25 22:54
唐汉 发表于 2023-3-25 06:46
[mw_shl_code=fortran,true]program main

   implicit none

谢谢你!原来它需要先分配main里面的数组长度,然后再分配type里元素的长度,我顺序搞反了。
作者: 愤怒的三炮    时间: 2023-3-25 22:57
kyra 发表于 2023-3-25 08:00
根据我的习惯,做了一些修改。

我的有些习惯不是必须的,但我个人认为会让更清晰。

很受用,谢谢!
我是想读地震记录的,想着先确定好每一道的数组大小(type里的数组元素),然后再分配总共有多少地震道。
看来计算机的逻辑并不是这样的。
作者: 愤怒的三炮    时间: 2023-3-25 22:59
weixing1531 发表于 2023-3-25 12:08
二维不等长数组的固定套路

是的,本质就是一个二维的不等长数组。
作者: 愤怒的三炮    时间: 2023-3-26 01:48
本帖最后由 愤怒的三炮 于 2023-3-26 02:04 编辑
kyra 发表于 2023-3-25 08:00
根据我的习惯,做了一些修改。

我的有些习惯不是必须的,但我个人认为会让更清晰。

我想用这个派生类型数组去读取SEGY文件,但是提示错误
[Fortran] 纯文本查看 复制代码
type trace
      sequence
      type(segy_trace_head):: header
      real(4),dimension(:),allocatable:: idata
end type

type(segy_reel_head):: reel_head
type(trace),dimension(:),allocatable:: traces
integer:: fp,fid_segy,fid_traces
integer:: i
   
open(newunit=fid_segy,file='D:/Example.SEGY',form='unformatted',access='stream',convert='big_endian')
   
allocate(traces(500))
do i = 1,500
     allocate(traces(i)%idata(6000))
end do
   
read(fid_segy) reel_head
   
do i = 1, 500
      
     read(fid_segy) traces(i) ! 读取前500道
     
end do


Error: Data transfer element at (1) cannot have ALLOCATABLE components unless it is processed by a defined input/output procedure

出错位置在第23行,该怎么处理呢?


作者: kyra    时间: 2023-3-26 11:42
改成
read(fid_segy) traces(i)%header ,traces(i)%idata ! 读取前500道
就行了。
带有可分配数组的 type,不能整体读写。
作者: 风平老涡    时间: 2023-3-26 11:50
本帖最后由 风平老涡 于 2023-3-26 11:53 编辑
愤怒的三炮 发表于 2023-3-26 01:48
我想用这个派生类型数组去读取SEGY文件,但是提示错误
[mw_shl_code=fortran,true]type trace
      seque ...

read语句不能直接读取type类变量。


do i = 1, 500
  read(fid_segy) traces(i) % header, (traces(i) % idata(j), j = 1, 6000)
end do
作者: kyra    时间: 2023-3-26 15:14
风平老涡 发表于 2023-3-26 11:50
read语句不能直接读取type类变量。

不含可分配数组和指针的派生类型是可以直接整体读写的。
作者: 愤怒的三炮    时间: 2023-3-27 00:19
kyra 发表于 2023-3-26 11:42
改成
read(fid_segy) traces(i)%header ,traces(i)%idata ! 读取前500道
就行了。

谢谢解答!
但感觉这样的特性好奇怪,不知道其它语言有没有这种限制。
作者: kyra    时间: 2023-3-27 08:30
抱歉,我对其他语言了解很少。
但是 C/C++ 也有同样的问题,如果包含指针(指向一个分配出来的数组),那么读取的是指针而不是指针指向的目标。
此外,C/C++的原生指针(非容器类),编译器并不存储指向目标数组的大小,所以更没法整体读取数组。




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2