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

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

问题描述

对于我的编程语言,一类hw问题问:



FORTRAN中的局部变量是静态还是堆栈动态?局部变量的INITIALIZED为默认值静态还是堆栈动态?告诉我一些代码与解释来备份你的答案。提示:检查这一点的最简单方法是让程序测试子程序的历史敏感度。看一下当你将本地变量初始化为一个值时,如果没有的话。您可能需要调用多个子程序来自信地锁定您的答案。



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



每次连续调用子程序打印出相同的随机当变量未初始化时,该值将打印出初始化值。



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



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



我的第二个子程序也创建一个变量,但是调用第一个子程序。结果是相同的,除了未初始化变量的随机数不同。我很困惑请帮助!



非常感谢你。

解决方案

在Fortran 77& 90/95/2003,如果要跨子程序调用保留子例程的本地变量的值,则应声明save属性,例如(使用Fortran 90样式):

  integer,save :: counter 

OR

  integer :: counter 
save :: counter
/ pre>


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

 保存

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

  integer :: counter = 0 

自动获取保存属性。我不认为在Fortran 77中是这样的。



这是一个实验可能会导致误导的领域 - 他们会告诉你一个特定的编译器,但也可能不是Fortran 77语言标准,也不是其他编译器。许多旧的Fortran 77编译器没有在堆栈上放置局部变量,隐含地,所有变量都具有save属性,而编程已经使用了该声明。例如,这是受欢迎的DEC Fortran编译器的情况。传统的Fortran 77程序通常只能使用这种类型的特定编译器与现代编译器发生故障,因为程序员忘记在需要的变量上使用save属性。原来这并没有引起问题,因为所有的变量都有save属性。大多数现代编译器都放置局部变量而不保存在堆栈中,这些程序经常发生故障,因为需要保存的某些变量在子程序调用中忘记其值。这可以通过识别问题变量并添加保存(工作),向每个子例程添加保存语句(较少的工作),或许多编译器有选项(例如,在gfortran中使用-fno-automatic)来恢复旧的行为容易)。



这似乎是一个特殊的问题 - 你不会发现Fortran 77,而是关于一个特定的编译器。为什么要使用Fortran 77而不是Fortran 95/2003?教授。认为Fortran在1977年停止了?


For my programming languages class one hw problem asks:

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?

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!

Thank you so much.

解决方案

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

OR

integer :: counter
save :: counter

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

save

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

integer :: counter = 0

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

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).

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天全站免登陆