将派生类型中的指针分配给 Fortran 中相同类型中的目标

2024-04-06

我想在包含在同一派生类型中的派生类型中分配一个指针。下面的代码给了我下面的错误。这是怎么回事,我该如何解决这个问题?

   24 |         zoos(i)%tigers(1) => zoos(i)%animals(1, 1)
      |        1
Error: Expected bounds specification for 'zoos' at (1)
module mo_zoo
    implicit none
    type zoo
        integer, dimension(:,:), pointer :: animals
        integer, dimension(:), pointer :: tigers
        integer, dimension(:), pointer :: ducks
    end type zoo
   
    save
        type(zoo), dimension(:), pointer :: zoos
end module mo_zoo

program test
    use mo_zoo
    implicit none
    integer :: n_zoos
    integer :: i

    n_zoos = 4
    allocate(zoos(n_zoos))

    do i = 1, n_zoos
        allocate(zoos(i)%animals(10, 2))
        zoos(i)%tigers(1) => zoos(i)%animals(1, 1)
        zoos(i)%ducks(1) => zoos(i)%animals(1, 2)
    end do
end program test

该问题与派生类型无关,并且错误消息是错误的。

问题是zoos(i)%ducks是一个指向数组的指针,而不是指针数组,所以你需要指向zoos(i)%ducks at zoos(i)%animals(:, 2), 而不是zoos(i)%ducks(1) at zoos(i)%animals(1, 2).

我之前谈过这个在这个答案中 https://stackoverflow.com/questions/67033612/array-of-pointers-in-fortran/67056904#67056904.

我相信这符合您的要求:

module mo_zoo
    implicit none
    type zoo
        integer, dimension(:,:), pointer :: animals
        integer, dimension(:), pointer :: tigers
        integer, dimension(:), pointer :: ducks
    end type zoo
   
    save
        type(zoo), dimension(:), pointer :: zoos
end module mo_zoo

program test
    use mo_zoo
    implicit none
    integer :: n_zoos
    integer :: i

    n_zoos = 4
    allocate(zoos(n_zoos))

    do i = 1, n_zoos
        allocate(zoos(i)%animals(10, 2))
        zoos(i)%tigers => zoos(i)%animals(:, 1)
        zoos(i)%ducks => zoos(i)%animals(:, 2)
    end do
end program test

我还想提出一个框架挑战。正如评论中(尤其是 Ian Bush)所指出的,Fortran 中的指针非常容易出错。

我建议更换任何分配的pointers with allocatables。这些allocatable还需要target如果他们有pointers 指着他们,就像这样:

module mo_zoo
    implicit none
    type zoo
        integer, dimension(:,:), allocatable :: animals
        integer, dimension(:), pointer :: tigers
        integer, dimension(:), pointer :: ducks
    end type zoo
   
    save
        type(zoo), dimension(:), allocatable, target :: zoos
end module mo_zoo

program test
    use mo_zoo
    implicit none
    integer :: n_zoos
    integer :: i

    n_zoos = 4
    allocate(zoos(n_zoos))

    do i = 1, n_zoos
        allocate(zoos(i)%animals(10, 2))
        zoos(i)%tigers => zoos(i)%animals(:, 1)
        zoos(i)%ducks => zoos(i)%animals(:, 2)
    end do
end program test

使用有很多优点allocatables over pointer尽可能:

  • The memory held by allocatables will automatically be freed when the allocatables drop out of scope1.
  • 编译器可以做出更强有力的假设allocatable比大约pointers,有时会导致更快的代码。

1 At least, this is true according to the Fortran standard. There are several outstanding problems with some compilers (notably this bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90068) relating to finalisation.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将派生类型中的指针分配给 Fortran 中相同类型中的目标 的相关文章

  • SELECT TYPE 构造中的多态性分配

    我试图定义一个分配不同类型数组的子例程 这是代码的简化版本 subroutine Allocation1 Vec class allocatable intent out Vec select type Vec type is real 8
  • 在 Fortran 90 中,是否必须事先声明数组维度?

    是否有必要在任何其他代码之前声明数组维度 例如 我编写了以下简化的示例代码 PROGRAM mytest IMPLICIT NONE INTEGER i j k mysum Let array c be a k by k 2 array D
  • 使用 Visual Studio 2013 和 Intel Fortran 编译混合 C++/C 代码

    我正在尝试编译一个简单的 C Fortran 混合程序 但存在链接问题 我使用的是Visual Studio 2013 Ultimate和Intel Visual Fortran Compiler XE 14 该程序非常简单 是从网上的某个
  • 使用命令行查找数据文件的行数

    有一种常规方法 逐行读取并检查iostat每次读数时都会达到非零或负值 不过 我想打电话system command 例行公事和 使用wc l命令来计算数量 然后想要分配要放置数据的数组的维度 例如 我以两种方式打印行数 Program T
  • Fortran 2003,选择类型以区分“实数”和“实数数组”

    我的问题是 可以select type用块来区分real realInput from real realArrayInput 很清楚如何select type可以用于区分派生类型 但对我来说不太清楚它如何 或是否 可以用于内在类型 在 M
  • fortran中双引号和单引号的区别?

    我刚刚开始使用 Fortran 对双引号和单引号的使用感到困惑 它们是等价的 它们的用法没有区别 您可以使用它来打印引号字符之一 print print 首先打印 进而 注意 您还可以在一行中使用两个引号字符来打印一个 print prin
  • 如何包装 fortran write 语句

    我想包装 fortran写语句 http software intel com sites products documentation doclib stdxe 2013 composerxe compiler fortran lin 在
  • Fortran 中的数组第一个索引

    我认为 Fortran 中数组的第一个索引是 1 但是为什么这段代码可以工作呢 代码是 Wavewatch 的修改部分 http polar ncep noaa gov waves wavewatch http polar ncep noa
  • 如何将mortran代码转换为fortran代码

    我有一些 Mortran 代码 来自 glmnet 我想阅读和编译 我知道在编译时 Mortran首先转换为Fortran 然后编译 如果有预处理器的话 如何安装 Mortran 预处理器 特别是 OS X 上的 Mortran3 我在以下
  • 指定 gfortran 应该在其中查找模块的目录

    我目前基于模块来编译程序 例如主程序foo这取决于模块bar 如下 gfortran c bar f90 gfortran o foo exe foo f90 bar o 当foo f90 and bar f90位于同一目录中 如何指定 g
  • 将数组从 .npy 文件读入 Fortran 90

    我使用 Python 以二维数组 例如 X 的形式生成一些初始数据 然后使用 Fortran 对它们进行一些计算 最初 当数组大小约为 10 000 x 10 000 时 np savetxt 在速度方面表现良好 但是一旦我开始增加数组的维
  • 如何读取 Fortran 中内容不以空格分隔的 2D 文件

    我有一个矩阵存储在文件 number txt 中 如下所示 12323456 54254311 76534522 我如何在 Fortran 中读取这样的矩阵 结果将是 1 2 3 2 3 4 5 6 5 4 2 5 4 3 1 1 7 6
  • Fortran gfortran linux 中的“分段错误(核心转储)”错误

    我正在创建一个程序 该程序将分析目录中的文件 fits 然后它将在另一个目录中创建另一个文件 txt 它只是一个转换器 当我尝试执行该程序 编译正常 时 它给了我一条错误消息 程序收到信号 SIGSEGV 分段错误 无效的内存引用 此错误的
  • 使用 Fortran 进行数组问题的二分查找

    我正在使用 Schaum 的 Fortran 77 编程概要 一书 其中有一个关于使用括号值组方法进行二分搜索的示例 首先这是代码 INTEGER X 100 INTEGER RANGE INTEGER START FINISH PRINT
  • GO TO 语句 - Fortran 到 Matlab

    我一直在努力将此网格搜索代码从 Fortran 转换为 Matlab 但是我无法正确合并 GO TO 语句 我正在尝试使用 while 循环 但我认为我需要其他东西来结束搜索 任何帮助将不胜感激 vmax 1 0E 15 amax G 1
  • Fortran 递归分段错误

    我必须设计并实现一个 Fortran 例程来确定方格上簇的大小 并且递归地编写子例程似乎非常方便 然而 每当我的晶格大小超过某个值 大约 200 边 时 子例程就会始终出现段错误 这是我的集群检测例程 RECURSIVE SUBROUTIN
  • 这些双精度值如何精确到小数点后 20 位?

    当精度是一个问题时 我正在测试一些非常简单的等价错误 并希望以扩展双精度执行操作 这样我就知道答案在 19位数字中 然后以双精度执行相同的操作 其中第 16 位会有舍入误差 但不知何故 我的双精度算术保持了 19 位精度 当我在扩展双精度中
  • Fortran90 中 BLAS 函数返回零

    我正在学习在Fortran90中使用BLAS 并使用子例程编写了一个简单的程序SAXPY https software intel com en us mkl developer reference fortran axpy和函数SNRM2
  • Fortran DLL 导入

    Fortran 中有一段代码罗伯特 L 帕克和菲利普 B 斯塔克 http www stat berkeley edu 7Estark Code sbvq f FORTRAN subroutine bv key m n a b bl bu
  • 派生类型数组:选择条目

    目前在我的代码中我有一个二维数组 integer allocatable elements 并定义一些常量 integer parameter TYP 1 integer parameter WIDTH 2 integer paramete

随机推荐