声明和 SAVE 属性上的 Fortran 赋值陷阱
在 fortran 95 中,如果您在声明时分配一个变量
integer :: var = 0
,则该变量相当于
integer, save :: var = 0
在例程执行后保留该变量(相当于 C 语言中的static
),并且在再次调用时不会重新初始化。这种(恕我直言,危险)行为背后的基本原理/技术问题是什么?
In fortran 95, if you assign a variable at declaration
integer :: var = 0
it is equivalent to
integer, save :: var = 0
and the variable is therefore preserved after routine execution (is equivalent to static
in C speak) and does not get reinitialized when called again. What is the rationale/technical issue behind such (IMHO dangerous) behavior ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这种行为背后没有任何理由。
但据我所知,斯特凡诺,你使用了错误的术语。在您的代码中没有赋值语句,只有使用初始化表达式 (0) 的变量 (var) 初始化。
所以看起来这只是委员会的设计决定。如果我们有这样的表达式(在类型声明语句中带有等号),那么它是初始化而不是赋值。并且初始化仅在程序(而不是过程)执行期间发生一次。
然而,做出这样的决定可能有一些历史原因。看看这个线程。
如今,这种行为是危险的,因为许多其他广泛使用的语言遵循有关初始化/赋值的另一种约定。
I don't think that there is some rationale behind such behavior.
But as far as I know, Stefano, you used wrong terminology. In your code there is no assignment statement only variable (var) initialization using initialization expression (0).
So it seems that it was just committee design decision. If we have such expression (with equality sign in type declaration statement) it is initialization not assignment. And initialization takes place only once during program (and not procedures) execution.
However there might be some historical reasons for such decision. Take a look at this thread.
Today such behavior is dangerous because many other widely used languages follows another conventions about initialization/assignment.
许多旧的 FORTRAN 77 和更早版本的编译器静态分配所有变量。许多程序员依赖于这种行为——这在技术上是他们程序中的一个错误,因为除非他们在声明中使用“SAVE”限定符(或向每个过程添加一个简单的 SAVE 语句),否则变量的值在重新输入时是未定义的。程序。但由于当时的程序多年来往往与特定的平台和编译器绑定在一起,所以程序员就摆脱了这一点。将旧版 FORTRAN 77 代码移植到现代 Fortran >= 90 编译器时,这是一个非常常见的“陷阱”。大多数编译器提供编译时开关来恢复此行为,例如 gfortran 的 fno-automatic 选项。最有可能的是,委员会认为在其声明中初始化的变量很可能需要 SAVE 属性——在我看来,这是一个合理的设计决策。我认为与其他语言最不同的地方,也是最容易让多语言程序员感到困惑的是,初始化只完成一次。
Many old FORTRAN 77 and earlier compilers statically allocated all variables. Many programmers relied upon this behavior -- this was technically a bug in their programs since unless they used the "SAVE" qualifier in the declaration (or added a plain SAVE statement to every procedure) the value of the variable was undefined upon reentry to a procedure. But since in those days programs tended to be tied to a particular platform and compiler for years, programmers got away with this. This is a very common "gotcha" in porting legacy FORTRAN 77 code to a modern Fortran >= 90 compilers. Most compilers provide compile-time switches to restore this behavior, such as the fno-automatic option of gfortran. Most likely the committee viewed variables that were initialized in their declaration as very likely to need the SAVE attribute -- in my opinion, a reasonable design decision. I think what is most different from other languages, and easiest to confuse the multi-lingual programmer, is that the initialization is done only once.