Pointers 如何在FORTRAN例程中为结构/数组创建句柄?

Pointers 如何在FORTRAN例程中为结构/数组创建句柄?,pointers,fortran,handler,Pointers,Fortran,Handler,我需要在子例程中为一个相当复杂的结构(这里替换为“real a(2)”)创建一个句柄,然后只将句柄/指针传回主例程。我还需要能够创建尽可能多的这些需要的结构。 问题是一旦指针返回到main,数据结构就会被释放。以下是我的尝试: program main implicit none integer i, j real a(2) ! This can be a complicated structure a=(/1.1,2.2/) call myalloc(a,i)

我需要在子例程中为一个相当复杂的结构(这里替换为“real a(2)”)创建一个句柄,然后只将句柄/指针传回主例程。我还需要能够创建尽可能多的这些需要的结构。 问题是一旦指针返回到main,数据结构就会被释放。以下是我的尝试:

  program main
  implicit none

  integer i, j
  real a(2) ! This can be a complicated structure

  a=(/1.1,2.2/)
  call myalloc(a,i)

  a=(/3.1,4.2/)
  call myalloc(a,j)

  call myprint(i)      
  call myprint(j)

  stop
  end
c-----------------------------------

  subroutine myalloc(a,i)
  implicit none

  real, pointer :: i(:) ! If I add "save" attribute I will get an error
  real, target :: a(2)

  allocate(i(2))
  i => a

  return
  end 
  subroutine myprint (i)
  implicit none

  real, pointer :: i(:)

  print *, i

  return
  end
c--------------------------------

  subroutine myalloc(a,i)
  implicit none

  real, pointer :: i(:) ! If I add "save" attribute I will get an error
  real, target :: a(2)

  allocate(i(2))
  i => a

  return
  end 
  subroutine myprint (i)
  implicit none

  real, pointer :: i(:)

  print *, i

  return
  end
c---------------------------------

  subroutine myalloc(a,i)
  implicit none

  real, pointer :: i(:) ! If I add "save" attribute I will get an error
  real, target :: a(2)

  allocate(i(2))
  i => a

  return
  end 
  subroutine myprint (i)
  implicit none

  real, pointer :: i(:)

  print *, i

  return
  end
结果是一些垃圾值: 3.93764868E-43 1.40129846E-45

或者我会得到: 在文件test.f的第41行(unit=6,file='stdout') 内部错误:列表\格式化\写入():错误类型


任何帮助都将不胜感激。

此代码有太多错误,几乎很难建设性地回答

如果我理解您要做的事情,那么子例程
myalloc
应该像复制构造函数一样工作—将内存分配到提供的指针上,然后将初始化参数的内容复制到分配的内存上。因此,myalloc中的关键错误是以下两行:

  allocate(i(2))
  i => a
在这里,您首先将内存分配给指针
i
,然后将
i
分配给指向
a
。在这种情况下,我为什么要分配呢?我认为这更接近于你想要做的:

subroutine myalloc(a,b,n)
  implicit none

  real, dimension(n) :: a
  real, pointer,dimension(:) :: b
  integer :: n

  allocate(b(n))
  b = a ! This copies the contents of the array a to the allocation for b

  return
end 
program main
    implicit none

    interface
        subroutine myalloc(a,b,n)
        real,dimension(n) :: a
        real,pointer,dimension(:) :: b 
        integer :: n
        end subroutine myalloc

        subroutine myprint(i)
        real,pointer,dimension(:) :: i
        end subroutine myprint
    end interface

    real,pointer,dimension(:) :: i, j
    real :: a(2)

    a=(/1.1,2.2/)
    call myalloc(a,i,2)

    a=(/3.1,4.2/)
    call myalloc(a,j,2)

    call myprint(i)
    call myprint(j)

    stop
end
然后在你的主程序中有一些无法解释的事情。为什么
i
j
声明为整数?当然,它们必须是指向实际数组的指针,如果目的是将它们传递给您的
myalloc
,对它们执行分配/复制操作,然后打印它们?如果是这样的话,那么将main更改为类似这样的内容应该更接近您想要做的事情:

subroutine myalloc(a,b,n)
  implicit none

  real, dimension(n) :: a
  real, pointer,dimension(:) :: b
  integer :: n

  allocate(b(n))
  b = a ! This copies the contents of the array a to the allocation for b

  return
end 
program main
    implicit none

    interface
        subroutine myalloc(a,b,n)
        real,dimension(n) :: a
        real,pointer,dimension(:) :: b 
        integer :: n
        end subroutine myalloc

        subroutine myprint(i)
        real,pointer,dimension(:) :: i
        end subroutine myprint
    end interface

    real,pointer,dimension(:) :: i, j
    real :: a(2)

    a=(/1.1,2.2/)
    call myalloc(a,i,2)

    a=(/3.1,4.2/)
    call myalloc(a,j,2)

    call myprint(i)
    call myprint(j)

    stop
end
通过这些更改,您应该在运行时获得以下信息:

$ ./pointer
   1.100000       2.200000    
   3.100000       4.200000    

这与您期望的输出更接近吗?

此代码有太多错误,几乎很难建设性地回答

如果我理解您要做的事情,那么子例程
myalloc
应该像复制构造函数一样工作—将内存分配到提供的指针上,然后将初始化参数的内容复制到分配的内存上。因此,myalloc中的关键错误是以下两行:

  allocate(i(2))
  i => a
在这里,您首先将内存分配给指针
i
,然后将
i
分配给指向
a
。在这种情况下,我为什么要分配呢?我认为这更接近于你想要做的:

subroutine myalloc(a,b,n)
  implicit none

  real, dimension(n) :: a
  real, pointer,dimension(:) :: b
  integer :: n

  allocate(b(n))
  b = a ! This copies the contents of the array a to the allocation for b

  return
end 
program main
    implicit none

    interface
        subroutine myalloc(a,b,n)
        real,dimension(n) :: a
        real,pointer,dimension(:) :: b 
        integer :: n
        end subroutine myalloc

        subroutine myprint(i)
        real,pointer,dimension(:) :: i
        end subroutine myprint
    end interface

    real,pointer,dimension(:) :: i, j
    real :: a(2)

    a=(/1.1,2.2/)
    call myalloc(a,i,2)

    a=(/3.1,4.2/)
    call myalloc(a,j,2)

    call myprint(i)
    call myprint(j)

    stop
end
然后在你的主程序中有一些无法解释的事情。为什么
i
j
声明为整数?当然,它们必须是指向实际数组的指针,如果目的是将它们传递给您的
myalloc
,对它们执行分配/复制操作,然后打印它们?如果是这样的话,那么将main更改为类似这样的内容应该更接近您想要做的事情:

subroutine myalloc(a,b,n)
  implicit none

  real, dimension(n) :: a
  real, pointer,dimension(:) :: b
  integer :: n

  allocate(b(n))
  b = a ! This copies the contents of the array a to the allocation for b

  return
end 
program main
    implicit none

    interface
        subroutine myalloc(a,b,n)
        real,dimension(n) :: a
        real,pointer,dimension(:) :: b 
        integer :: n
        end subroutine myalloc

        subroutine myprint(i)
        real,pointer,dimension(:) :: i
        end subroutine myprint
    end interface

    real,pointer,dimension(:) :: i, j
    real :: a(2)

    a=(/1.1,2.2/)
    call myalloc(a,i,2)

    a=(/3.1,4.2/)
    call myalloc(a,j,2)

    call myprint(i)
    call myprint(j)

    stop
end
通过这些更改,您应该在运行时获得以下信息:

$ ./pointer
   1.100000       2.200000    
   3.100000       4.200000    

这是否接近您预期的输出?

谢谢您的回答。我应该把我的问题解释得更清楚些。我需要有一个结构,它的细节对主程序是隐藏的(只在将成为封闭源代码库的子程序中可见)。所以我试着用一个整数来表示“I”和“j”。我在MPI库中看到过类似的情况,例如MPI_TYPE_COMMIT(TYPE,err),其中TYPE被声明为整数,并且可能指向一个结构。下面是另一个可行的尝试,但我不确定如果将“myMod”编译成二进制文件,是否可以配置“sType”组件。您可能希望使用
transfer
函数将指针从外部可见类型转换为库中使用的内部类型。从形式上讲,Fortran 90只支持强类型指针,因此您可能正在编译器/运行时特定的beahviours中尝试这样做。如果您能够支持F2003,那么互操作性扩展可能值得研究,这样您就能够处理正确的void指针,如在C中。我必须说,从您最初的问题来看,这些都不是显而易见的……谢谢您的回答。我应该把我的问题解释得更清楚些。我需要有一个结构,它的细节对主程序是隐藏的(只在将成为封闭源代码库的子程序中可见)。所以我试着用一个整数来表示“I”和“j”。我在MPI库中看到过类似的情况,例如MPI_TYPE_COMMIT(TYPE,err),其中TYPE被声明为整数,并且可能指向一个结构。下面是另一个可行的尝试,但我不确定如果将“myMod”编译成二进制文件,是否可以配置“sType”组件。您可能希望使用
transfer
函数将指针从外部可见类型转换为库中使用的内部类型。从形式上讲,Fortran 90只支持强类型指针,因此您可能正在编译器/运行时特定的beahviours中尝试这样做。如果您能够支持F2003,那么互操作性扩展可能值得研究,这样您就能够处理正确的void指针,如在C中。我必须说,从您最初的问题来看,这一切都不明显。。。。