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

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

问题描述

是它可以分配在子程序的共同阵列的大小和值,然后使用它从程序的其他子程序?

下面的程序不工作,但我想要做这样的事情:

main.f

 程序的主整数n
整数,可分配::合作(:)常见的N,合作呼叫分配打印*,CO(1),CO(2)DEALLOCATE(共)
停止
结束程序的主

assign.f

 子程序分配整数n
整数,可分配::合作(:)常见的N,合作N = 2
分配(CO(N))合(1)= 1
共(2)= 2返回
结束子程序分配


解决方案

没有。您可以将指针成普通,但不a​​llocatables。

原因在于共同的基本概念是存储的关联,在那里你可以使一切都在共同的,然后将这些序列之中范围分享的内容的连续序列。 Allocatables可以有自己的大小在一个范围内,这会使得对事物的序列中跟踪在后可分配相当困难随附的通用块动态地变化。

(典型实施allocatables意味着直接与分配的相关联的存储仅仅是一个描述符 - 实际数据别处保持这实际上破坏了存储单元的连续序列的概念,因为分配状态(记录。在描述符)和数据都是可分配的值的两个组成部分。用于指针的执行是相似的,但随后在概念上是在存储器别处中的数据不是指针的值的一部分,因此它不应该被预期出现在该共同描述了连续序列 - 指针的序列中,但是不知道它在点)

Allocatables需要F90。这意味着,你可以使用模块的变量 - 这是比使用普通全局数据远更好的解决方案。如果你必须做到这一点使用常见的,然后使用一个数据指针。

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.

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.)

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天全站免登陆