Fortran 中常见的块用法
我是 Fortran 新手,只是为了工作做一些简单的事情。一般来说,作为一名新程序员,不确定它到底是如何工作的,所以如果我的解释或符号不是最好的,请原谅。 .F 文件的顶部有通用声明。向我解释它的人说,把它想象成 C 中的结构,并且它们是全局的。同样在同一个 .F 文件中,他们用什么类型声明了它。所以它是这样的:
COMMON SOMEVAR
INTEGER*2 SOMEVAR
然后当我实际看到它在其他文件中使用时,他们声明局部变量(例如 SOMEVAR_LOCAL),并根据条件设置 SOMEVAR_LOCAL = 1 或 0。
然后还有另一个条件行会说类似的话
IF (SOMEVAR_LOCAL. eq. 1)
SOMEVAR(PARAM) = 1;
(如果这不是正确的 Fortran,我再次道歉,但我现在无法访问代码)。所以在我看来,有一个类似“结构”的变量,名为 SOMEVAR,它有一定的长度(2 个字节的数据?),然后有一个局部变量用作标志,以便稍后,全局变量struct SOMEVAR 可以设置为该值。但是因为有(PARAM),所以它就像该特定实例的数组?谢谢。抱歉我的解释不好,但希望你能理解我的要求。
I'm new to Fortran and just doing some simple things for work. And as a new programmer in general, not sure exactly how this works, so excuse me if my explanation or notation is not the best. At the top of the .F file there are common declarations. The person explaining it to me said think of it like a struct in C, and that they are global. Also in that same .F file, they have it declared with what type. So it's something like:
COMMON SOMEVAR
INTEGER*2 SOMEVAR
And then when I actually see it being used in some other file, they declare local variables, (e.g. SOMEVAR_LOCAL) and depending on the condition, they set SOMEVAR_LOCAL = 1 or 0.
Then there is another conditional later down the line that will say something like
IF (SOMEVAR_LOCAL. eq. 1)
SOMEVAR(PARAM) = 1;
(Again I apologize if this is not proper Fortran, but I don't have access to the code right now). So it seems to me that there is a "struct" like variable called SOMEVAR that is of some length (2 bytes of data?), then there is a local variable that is used as a flag so that later down the line, the global struct SOMEVAR can be set to that value. But because there is (PARAM), it's like an array for that particular instance? Thanks. Sorry for my bad explanation, but hopefully you will understand what I am asking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是为了放大 @MSB 已经提到的内容:COMMON 块告诉编译器如何将变量放置在内存中。几乎没有理由将它们与现代 Fortran(即任何可以处理 Fortran 90 或更高版本的编译器)一起使用,并且有充分的理由避免它们。
还要添加一件事:在现代 Fortran 中,您可以执行类似于 C 结构体对用户定义类型所做的操作。检查您的文档中的 TYPE。
Just to amplify something @MSB already mentioned: COMMON blocks tell a compiler how to lay variables out in memory. There is almost no reason to use them with modern Fortran, ie with any compiler which can cope with Fortran 90 or later, and there are good reasons to avoid them.
And to add one thing: in modern Fortran you can do approximately what C structs do with user defined types. Check your documentation for TYPE.
第一个声明将 SOMEVAR 作为两个字节的标量整数。您显示的用法有 SOMEVAR 有一个数组 - 基于它被索引。在 Fortran 中可以通过“序列关联”来做到这一点,但这是不好的做法。在一个文件中,您可以将 SOMEVAR 声明为 INTEGER*2,并向该标量分配两个字节。在另一个文件中,您可以将其声明为 INTEGER*1 SOMEVAR(2),并保留两个字节,这次是一个包含两个元素的数组,每个元素一个字节。在两个文件中使用相同的公共块可能会导致这两个变量逐字节重叠——序列关联。许多年前,当内存非常小时,程序员这样做是为了减少内存使用,因为他们知道不同的子例程在不同的时间使用变量。今天这样做的原因非常非常有限。大多数情况下不应该这样做,因为它很容易造成混乱。
您还可以使用 EQUIVALENCE 语句设置序列关联。再次强调,最好避免。在必须执行需要 EQUIVALENCE 语句的“棘手”事情的时代,现代替代方法是 TRANSFER 函数。
The first declaration has SOMEVAR as a scalar integer of two bytes. The usage you show has SOMEVAR has an array -- based on it being indexed. This is possible to do in Fortran via "sequence association" but it is poor practice. In one file you could declare SOMEVAR as INTEGER*2 and two bytes are allocated to this scalar. In another file you could declare it as INTEGER*1 SOMEVAR(2), and two bytes are reserved, this time for an array of two elements, each of one byte. Using the same common block in both files can cause these two variables to overlap, byte by byte -- sequence association. Many years ago, when memory was very small, programmers did this to reduce memory usage, knowing that different subroutines were using variables at different times. The reasons to do this today are very, very limited. Mostly one shouldn't because it is liable to be confusing.
You can also setup sequence association with the EQUIVALENCE statement. Again, best avoided. The modern replacement for the times that one must do "tricky" things that needed the EQUIVALENCE statement is the TRANSFER function.