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

发布于 2024-11-28 10:21:36 字数 1008 浏览 0 评论 0原文

我需要在子例程中为相当复杂的结构创建一个句柄(此处替换为“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 

c-------- ------------------------

  subroutine myprint (i)
  implicit none

  real, pointer :: i(:)

  print *, i

  return
  end

c------------------------- --------

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

或者我会得到: 在文件 test.f 的第 41 行(单位 = 6,文件 = 'stdout') 内部错误:list_formatted_write():类型错误

任何帮助将不胜感激。

I need to create an handle for an fairly complicated structure (here replaced with "real a(2)") in a subroutine and then only pass back the handle/pointer to the main routine. I also need to be able to create as many as of these structure that are needed.
The problem is that the data structure will be deallocated once the pointer is returned to main. Here is my attempt:

  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 

c--------------------------------

  subroutine myprint (i)
  implicit none

  real, pointer :: i(:)

  print *, i

  return
  end

c---------------------------------

The result is some garbage values:
3.93764868E-43 1.40129846E-45

Or I would get:
At line 41 of file test.f (unit = 6, file = 'stdout')
Internal Error: list_formatted_write(): Bad type

Any help would be highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的故事 2024-12-05 10:21:36

这段代码有太多错误,几乎很难做出建设性的回答。

如果我理解你想要做什么,子例程 myalloc 应该像一个复制构造函数一样 - 将内存分配到提供的指针上,然后将初始化参数的内容复制到分配的内存上。所以 myalloc 中的关键错误是这两行:

  allocate(i(2))
  i => a

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

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 

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

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    

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

There is so much wrong with this code it is almost too difficult to answer constructively.

If I understand what you are trying to do, the subroutine myalloc is supposed to act like a copy constructor of sorts - allocate memory onto a supplied pointer and then copy the contents of an initializing argument onto the memory allocated. So the key error in your myalloc is these two lines:

  allocate(i(2))
  i => a

Here you first allocate memory to the pointer i, then assign i to point to a. Why allocate i at all in this case? I presume this is closer to what you are trying to do:

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 

Then in your main program there are a number of inexplicable things. Why are i and j declared as integers? Surely they must be pointers to real arrays, if the intention is to pass them to your myalloc, perform the allocation/copy operation on them, then print them? If that is the case, then changing your main to something like this should be closer to what it seems you are trying to do:

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

With these changes, you should get this when run:

$ ./pointer
   1.100000       2.200000    
   3.100000       4.200000    

Is that closer to what you were expecting as output?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文