Fortran 77 中的局部变量是静态的还是堆栈动态的?

发布于 2024-08-28 04:03:26 字数 471 浏览 8 评论 0原文

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

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

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

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

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

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

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

太感谢了。

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.

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

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

发布评论

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

评论(2

瞎闹 2024-09-04 04:03:26

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

integer, save :: counter

OR

integer :: counter
save :: counter


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

save

不带任何变量的简单语句即可。
在 Fortran 90 中,声明中的变量初始化

integer :: counter = 0

会自动获取 save 属性。我认为 Fortran 77 中的情况并非如此。

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

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

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?

凝望流年 2024-09-04 04:03:26

放大 @MSB 提出的一个观点;

Fortran 标准并不告诉编译器编写者如何实现这些标准,它们关心的是程序员可见的程序行为。所以问题的答案是“这一切都取决于编译器”。并且 OP 没有告诉我们他正在使用哪个编译器。

此外,如果您回顾过去的历史,检查所有曾经编写的 FORTRAN77 编译器,我相信您会发现您感兴趣的功能的各种不同实现,其中许多与相当深奥的硬件架构相关。

To amplify on one point that @MSB made;

Fortran standards do not tell compiler-writers how to implement the standards, they are concerned with the behaviour of programs visible to the programmer. So the answer to the question is 'it all depends on the compiler'. And OP does not tell us which compiler(s) (s)he is using.

Furthermore, if you trawl back through the mists of time to examine all the FORTRAN77 compilers ever written, I am confident that you will find a wide variety of different implementations of the features you are interested in, many of them tied to quite esoteric hardware architectures.

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