Fortran 90 数组按引用传递问题
如果我这样分配 F90 指针会发生什么:
real, pointer :: abc(:)
allocate abc (nx*ny*nz)
我将 abc 传递给子例程,在子例程中我将其重新定义为
real arg1(nx,ny,xz)
这似乎工作正常。
但是如果我重新定义为二维数组,就会出现段错误。
real arg1(nx,ny)
使用上面重新排序的数组,它应该可以工作。为什么会失败?任何帮助将不胜感激。
谢谢。
What happens if I allocate a F90 pointer thus:
real, pointer :: abc(:)
allocate abc (nx*ny*nz)
I pass abc to a subroutine, where i redefine it as
real arg1(nx,ny,xz)
This seems to work fine.
But if I redefine as 2D array, I get a segfault.
real arg1(nx,ny)
With reordered arrays as above, it should work. Why does it fail? Any help will be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它失败是因为你在数组的大小上向编译器撒谎。
在 Fortran 77 中,人们总是使用这些技巧,因为别无选择。在我们当前开明的时代,我们永远不应该做这些伎俩——只有 Fortran77 和 C 程序员才不得不诉诸这种诡计。
根据 更改 fortran 中的数组尺寸 的答案,如果您想调整数组大小,或者只是使用不同的形状创建对它的引用,您可以使用 RESHAPE 内在函数,或使用数组指针。
It fails because you're lying to the compiler about the size of the array.
In Fortran 77, people used to use the tricks all the time, because there was no option. In our current enlightened age, we should never do these sorts of tricks -- only Fortran77 and C programmers have to resort to such skulduggery.
As per the answers to changing array dimensions in fortran , if you want to resize an array, or just create a reference to it with a different shape, you can use the
RESHAPE
intrinsic, or use an array pointer.我不太明白你的问题,你所做的应该有效,因为在这种情况下只传递了对数组的引用。
要了解数组是如何传递的,您可以阅读 此页面。在底部您可以找到包含可能情况的表格。这里适用左下角的情况。
I don't really understand your problem, what you do should work, since in this case only a reference to the array is passed.
To know how the arrays are passed, you can read this page. At the bottom you can find a table with possible situations. Here the bottom left case applies.