Fortran 公共变量,可分配数组 [英] Fortran common variables, allocatable array

查看:11
本文介绍了Fortran 公共变量,可分配数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在子程序中分配公共数组的大小和值,然后在程序的其他子程序中使用它?

is it possible to assign the size and values of a common array in a subroutine and then use it from other subroutines of the program?

以下程序不起作用,但我想做这样的事情:

The following program doesn't work, but I want to do something like this:

main.f

program main

integer n
integer, allocatable :: co(:)

common n, co

call assign

print *, co(1), co(2)

deallocate(co)
stop
end program main

assign.f

subroutine assign

integer n
integer, allocatable :: co(:)

common n, co

n = 2
allocate(co(n))

co(1) = 1
co(2) = 2

return
end subroutine assign

推荐答案

没有.你可以把指针放到公共的,但不是可分配的.

No. You can put pointers into common, but not allocatables.

原因是通用的基本概念是存储关联,您可以在其中创建通用的所有事物的连续序列,然后在范围之间共享这些序列.Allocatable 的大小可以在一个范围内动态变化,这将使得跟踪可分配之后的公共块中的事物序列相当困难.

The reason is that a concept fundamental to common is storage association, where you can make a contiguous sequence of all the things that are in the common and those sequences are then shared amongst scopes. Allocatables can have their size vary dynamically in a scope, which would make the tracking in the sequence of things in the common block that came after the allocatable rather difficult.

(可分配的典型实现意味着与可分配直接关联的存储只是一个描述符 - 实际数据保存在其他地方.这实际上打破了存储单元连续序列的概念,因为分配状态(如记录在描述符中)和数据都是可分配值的一部分.指针的实现是相似的,但是从概念上讲,内存中其他地方的数据不是指针值的一部分,因此不应期望出现在公共描述的连续序列中 - 指针在序列中,但不是它指向的位置.)

(Typical implementation of allocatables means that the storage directly associated with the allocatable is just a descriptor - the actual data is kept elsewhere. This practically breaks the concept of a contiguous sequence of storage units, given that the allocation status (as recorded in the descriptor) and the data are both part of the value of the allocatable. The implementation for pointers is similar, but then conceptually the data that is elsewhere in memory is not part of the value of the pointer, so it should not be expected to appear in the contiguous sequence that the common describes - the pointer is in the sequence, but not what it points at.)

可分配项需要 F90.这意味着您可以使用模块变量——这是一个比对全局数据使用 common 更好的解决方案.如果您必须使用 common 执行此操作,请使用数据指针.

Allocatables require F90. That means that you can use module variables - which are a far better solution than the use of common for global data. If you must do this using common, then use a data pointer.

这篇关于Fortran 公共变量,可分配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆