在Fortran中对DIMENSION CLIPG(1)语句的概念性理解 [英] Conceptual understanding of DIMENSION CLIPG(1) statement in Fortran

查看:306
本文介绍了在Fortran中对DIMENSION CLIPG(1)语句的概念性理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一些遗留代码时,我发现了以下Fortran函数声明。下面的代码片段显示了函数声明和参数声明。我相信Fortran是一种不区分大小写的语言。

 子路径CLIP2G(fcut,TIME,NUMS,NUMG,CLIPG,CLIPGL, CLIPGR,MODE,PHZ)

真正的fcut,TIME,
整数NUMS,NUMG
DIMENSION CLIPG(1)
REAL clipgr(1),clipgl(1)
INTEGER MODE
LOGICAL PHZ

DIMENSION CLIPG(1)语句?



我发现一个链接对陈述的一个相当简洁的解释,但主要作为C / C ++程序员,我觉得这个概念有点难以理解。请注意, REAL clipgr(1),clipgl(1)后面是一个括号(1)。这是一个长度= 1的数组,类型为 REAL



Stack Overflow还有其他一些链接,但即使在下面链接的帖子中给出的类C语法中,我也不确定这个意思。

Fortran函数说明



也许 DIMENSION CLIPG(1)相当于 REAL CLIPG 语句?什么是最接近的C语言等值?

解决方案

使用DIMENSION 向编译器表明该变量是一个数组。在这种情况下, DIMENSION CLIPG(1)声明 CLIPG 是一个元素的数组。它也被隐式输入为 REAL ,所以一个等效的声明是:

  REAL CLIPG(1)

等价的C构造将会是

  ...,float clipg [1],... 






请注意,这个可能并不意味着 CLIPG 确实是只有一个元素的数组。它也可以作为(非常糟糕的)方式向编译器解释: CLIPG 是一个不同大小的数组(例如,您可以使用数组调用该子例程5个元素,然后再次以50000个元素的数组)。它被声明为只有一个元素,所以编译器知道它是一个数组,但是如果实际参数是一个更大的数组,它可以以超出其结束的方式进行访问。这是一个非常糟糕的做法,但您可以在很多Fortran代码中找到它。



FORTRAN 77提供了另一种描述这种数组的方法:

  REAL CLIPG(*)

  DIMENSION CLIPG(*)

这种数组称为假定大小的数组。只有虚拟例程参数可以被声明为假定尺寸的数组,并且只有数组的最后一个维度可以被省略,例如

  DIMENSION CLIPG2D(10,*)

(但不是 DIMENSION CLIPG2D(*,10 )



这意味着 CLIPG2D 是一个 something x 10 矩阵(Fortran以矩阵方式存储矩阵)和 something 可能会有所不同。无论何时使用假定大小的数组,还必须明确地将省略的维数的大小提供给例程。



在C中,可以使用 float clipg [] float * clipg


When working with some legacy code, I've found the following Fortran function declaration. The snippet below shows both the function declaration and the declaration of parameters. I believe that Fortran is a case-insensitive language.

 SUBROUTINE CLIP2G (fcut,TIME,NUMS,NUMG,CLIPG,CLIPGL,CLIPGR,MODE,PHZ)

      real fcut, TIME,
      integer NUMS, NUMG
      DIMENSION CLIPG(1)
      REAL clipgr(1),clipgl(1)
      INTEGER MODE
      LOGICAL PHZ

What is the meaning of the DIMENSION CLIPG(1) statement?

I found a link to a rather concise explanation of the statement, but primarily as a C/C++ programmer, I find the concept somewhat challenging to understand. Note how REAL clipgr(1), clipgl(1) are followed by a bracket (1). Is this an array of length = 1 with type REAL?

There are some other links on Stack Overflow, but even in the C-like syntax given in the posting linked below I am uncertain with respect to the meaning.

Fortran Function explanation

Perhaps DIMENSION CLIPG(1) is equivalent to the REAL CLIPG statement? What is the closest C-language equivalent?

解决方案

DIMENSION is used to indicate to the compiler, that the variable is an array. In this case DIMENSION CLIPG(1) declares CLIPG as being an array of one element. It is also implicitly typed to be REAL, so an equivalent declaration would be:

REAL CLIPG(1)

The equivalent C construct would be

..., float clipg[1], ...


Note that this might not mean that CLIPG is really an array of just one element. It could also be used as (a very bad) way to explain to the compiler that CLIPG is an array of a varying size (e.g. you can call this subroutine once with an array of 5 elements and then again with an array of 50000 elements). It is declared as having just one element, so the compiler knows that it is an array, but then it could be accessed way beyond its end if the actual argument is a larger array. This is a really bad practice but you can find it used in lots of very old Fortran codes.

FORTRAN 77 provides another way to describe such arrays:

REAL CLIPG(*)

or

DIMENSION CLIPG(*)

Such arrays are called assumed-size arrays. Only dummy routine arguments can be declared as assumed-size arrays and only the last dimension of the array could be omitted, e.g.

DIMENSION CLIPG2D(10,*)

(but not DIMENSION CLIPG2D(*,10))

This means that CLIPG2D is a something x 10 matrix (Fortran stores matrices columnwise), and something could vary. Whenever assumed-size arrays are used, one also has to explicitly supply the size of the omitted dimension to the routine.

In C one would use either float clipg[] or float *clipg.

这篇关于在Fortran中对DIMENSION CLIPG(1)语句的概念性理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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