如何在 FORTRAN 例程中创建结构/数组的句柄?
我需要在子例程中为相当复杂的结构创建一个句柄(此处替换为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码有太多错误,几乎很难做出建设性的回答。
如果我理解你想要做什么,子例程 myalloc 应该像一个复制构造函数一样 - 将内存分配到提供的指针上,然后将初始化参数的内容复制到分配的内存上。所以 myalloc 中的关键错误是这两行:
这里首先将内存分配给指针
i
,然后分配i
指向a
。在这种情况下为什么要分配 i 呢?我认为这更接近您想要做的事情:然后在您的主程序中存在许多无法解释的事情。为什么
i
和j
声明为整数?当然,它们必须是指向真实数组的指针,如果目的是将它们传递给您的 myalloc,对它们执行分配/复制操作,然后打印它们?如果是这种情况,那么将您的 main 更改为类似这样的内容应该更接近您想要做的事情:通过这些更改,您应该在运行时得到:
这更接近您期望的输出吗?
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:Here you first allocate memory to the pointer
i
, then assigni
to point toa
. Why allocate i at all in this case? I presume this is closer to what you are trying to do:Then in your main program there are a number of inexplicable things. Why are
i
andj
declared as integers? Surely they must be pointers to real arrays, if the intention is to pass them to yourmyalloc
, 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:With these changes, you should get this when run:
Is that closer to what you were expecting as output?