“保存”一个公共块

发布于 2025-01-08 22:30:20 字数 292 浏览 5 评论 0原文

我正在处理一些广泛使用 COMMON 块并且有时使用 SAVE 语句的遗留代码。查阅 Fortran 标准后,它说:

SAVE 语句中出现前面和后面带有斜线的公共块名称具有指定该公共块中所有实体的效果。

在什么情况下将变量放在公共块中并不意味着SAVE?由于该变量必须可以在包含该公共块的任何其他程序单元中访问,因此它怎么可能不被SAVE编辑呢?

I'm dealing with some legacy code that uses COMMON blocks extensively and sometimes uses the SAVE statement. After consulting the Fortran standard, it says:

The appearance of a common block name preceded and followed by a slash in a SAVE statement has the effect of specifying all of the entities in that common block.

Under what circumstances does placing a variable in a common block not imply SAVE? Since the variable must be accessible in any other program unit that includes that common block, how could it not be SAVEed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

廻憶裏菂餘溫 2025-01-15 22:30:20

我必须查一下,因为我的印象和你一样。

似乎只有未命名的所谓空白公共块中的变量在整个程序中保留其定义状态。命名公共块中未保存的变量在从子程序返回时变为未定义,除非另一个当前活动的程序单元包含同一公共块的公共语句。

来自标准(Fortran 77,但最新的标准包含类似的措辞):

17.3 导致实体变得未定义的事件
[...]
6. 在子程序中执行 RETURN 语句或 END 语句会导致子程序中的所有实体变为未定义,但以下实体除外:
[...]
d.出现在子程序中并出现在至少一个直接或间接引用子程序的其他程序单元中的实体

I had to look it up, because I was under the same impression as you are.

It seems that only variables in an unnamed, so-called blank, common block retain their definition status across the entire program. Unsaved variables in a named common block become undefined on return from a subprogram, unless another currently active program unit includes a common statement for the same common block.

From the standard (Fortran 77, but the latest one contains similar wording):

17.3 Events That Cause Entities to Become Undefined
[...]
6. The execution of a RETURN statement or an END statement within a subprogram causes all entities within the subprogram to become undefined except for the following:
[...]
d. Entities in a named common block that appears in the subprogram and appears in at least one other program unit that is either directly or indirectly referencing the subprogram

留一抹残留的笑 2025-01-15 22:30:20

Fortran 77 时代的许多编译器“保存”了所有本地过程变量,无论是否指定了“SAVE”。这是遗留程序在现代编译器中失败的一个常见原因,当变量超出语言标准允许的范围时,现代编译器将取消定义变量。那些较旧的编译器可能还会在程序运行期间保留所有公共变量的值,即使语言标准并不要求这样做。

Many compilers of the Fortran 77 era "saved" all local procedure variables, whether or not "SAVE" was specified. This is a common reason for legacy programs to fail with modern compilers, which will undefine variables when they go out of scope, as allowed by the language standard. Probably those older compilers would also maintain the values of all common variables for the duration of the program run, even though that wasn't required by the language standard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文