“保存”一个公共块
我正在处理一些广泛使用 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 SAVE
ed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我必须查一下,因为我的印象和你一样。
似乎只有未命名的所谓空白公共块中的变量在整个程序中保留其定义状态。命名公共块中未保存的变量在从子程序返回时变为未定义,除非另一个当前活动的程序单元包含同一公共块的公共语句。
来自标准(Fortran 77,但最新的标准包含类似的措辞):
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):
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.