Fortran 77 中的局部变量是静态的还是堆栈动态的? [英] Are local variables in Fortran 77 static or stack dynamic?

查看:25
本文介绍了Fortran 77 中的局部变量是静态的还是堆栈动态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的编程语言第一类硬件问题:

For my programming languages class one hw problem asks:

FORTRAN 中的局部变量是静态的还是堆栈动态的?初始化为默认值的局部变量是静态的还是堆栈动态的?给我看一些带有解释的代码来支持你的答案.提示:检查这一点的最简单方法是让您的程序测试子程序的历史敏感性.看看当你将局部变量初始化为一个值时会发生什么,当你不初始化时会发生什么.您可能需要调用多个子程序才能自信地锁定答案.

Are local variables in FORTRAN static or stack dynamic? Are local variables that are INITIALIZED to a default value static or stack dynamic? Show me some code with an explanation to back up your answer. Hint: The easiest way to check this is to have your program test the history sensitivity of a subprogram. Look at what happens when you initialize the local variable to a value and when you don’t. You may need to call more than one subprogram to lock in your answer with confidence.

我写了几个子程序:- 创建一个变量- 打印变量- 将变量初始化为一个值- 再次打印变量

I wrote a few subroutines: - create a variable - print the variable - initialize the variable to a value - print the variable again

每次对子程序的连续调用都会在变量未初始化时打印出相同的随机值,然后打印出初始化值.

Each successive call to the subroutine prints out the same random value for the variable when it is uninitialized and then it prints out the initialized value.

当变量未初始化时,这个随机值是多少?

What is this random value when the variable is uninitialized?

这是否意味着 Fortran 每次调用子程序时都使用相同的内存位置,还是动态创建空间并随机初始化变量?

Does this mean Fortran uses the same memory location for each call to the subroutine or it dynamically creates space and initializes the variable randomly?

我的第二个子程序也创建了一个变量,但随后调用了第一个子程序.结果是一样的,只是打印的未初始化变量的随机数不同.我很迷茫.请帮忙!

My second subroutine also creates a variable, but then calls the first subroutine. The result is the same except the random number printed of the uninitialized variable is different. I am very confused. Please help!

非常感谢.

推荐答案

In Fortran 77 &90/95/2003,如果您希望在子例程调用中保留子例程局部变量的值,则应将其声明为save"属性,例如(使用 Fortran 90 样式):

In Fortran 77 & 90/95/2003, if you want the value of a variable local to a subroutine preserved across subroutine calls, you should declare it the "save" attribute, e.g., (using Fortran 90 style):

integer, save :: counter

integer :: counter
save :: counter

.或者,如果您希望保存"行为应用于所有变量,只需在子例程中包含一个简单的

. Or, if you want the "save" behavior to apply to all variables just include in the subroutine a simple

save

没有任何变量的语句.在 Fortran 90 中,声明中的变量初始化,

statement without any variables. In Fortran 90, a variable initialization in a declaration,

integer :: counter = 0

自动获取保存属性.我不认为 Fortran 77 是这种情况.

automatically acquires the save attribute. I don't think that this was the case in Fortran 77.

这是一个实验可能会产生误导的领域——它们会告诉您特定编译器的作用,但可能不会告诉您 Fortran 77 语言标准是什么,也不会告诉您其他编译器做了什么.许多旧的 Fortran 77 编译器没有将局部变量放在堆栈上,并且隐式地所有变量都具有 save 属性,编程人员没有使用该声明.例如,流行的 DEC Fortran 编译器就是这种情况.对于仅与此类特定编译器一起使用的旧版 Fortran 77 程序而言,在现代编译器中出现故障是很常见的,因为程序员忘记在需要它的变量上使用 save 属性.最初这不会引起问题,因为所有变量都有效地具有 save 属性.大多数现代编译器将局部变量放置在堆栈中而不保存,并且这些程序经常出现故障,因为一些需要保存"的变量在子程序调用中忘记"了它们的值.这可以通过识别问题变量并添加保存(工作)、向每个子程序添加保存语句(减少工作)或许多编译器有一个选项(例如,gfortran 中的 -fno-automatic)来恢复旧行为来修复(容易).

This is one area in which experiments could be misleading -- they will tell you what a particular compiler does, but perhaps not what the Fortran 77 language standard is, nor what other compilers did. Many old Fortran 77 compilers didn't place local variables on the stack and implicitly all variables had the save attribute, without the programming having used that declaration. This, for example, was the case with the popular DEC Fortran compilers. It is common for legacy Fortran 77 programs that were used only with a particular compiler of this type to malfunction with a modern compiler because programmers forgot to use the save attribute on variables that needed it. Originally this didn't cause a problem because all variables effectively had the save attribute. Most modern compilers place local variables without save on the stack, and these programs frequently malfunction because some variables that need "save" "forget" their values across subroutine calls. This can be fixed by identifying the problem variables and adding save (work), adding a save statement to every subroutine (less work), or many compilers have an option (e.g., -fno-automatic in gfortran) to restore the old behavior (easy).

这似乎是一个奇怪的问题——您不会找到有关Fortran 77"的信息,而是关于特定编译器的信息.为什么使用 Fortran 77 而不是 Fortran 95/2003?教授吗?认为 Fortran 在 1977 年就停止了?

It seems a peculiar question -- you won't find out about "Fortran 77" but about a particular compiler. And why use Fortran 77 instead of Fortran 95/2003? Does the prof. think Fortran stopped in 1977?

这篇关于Fortran 77 中的局部变量是静态的还是堆栈动态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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